Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to send ArrayBuffer #229

Closed
pranavwani opened this issue Jun 13, 2020 · 5 comments
Closed

How to send ArrayBuffer #229

pranavwani opened this issue Jun 13, 2020 · 5 comments

Comments

@pranavwani
Copy link

Description: How to send binary data, as Curl.option.POSTFIELDSonly allow sendingstringandCurl.option.HTTPPOST` allow to sending the form data

Request Payload:

{
  uuid: '7d0ce53d-a614-49e6-96d5-0d9054e82f46',
  method: 'post',
  url: 'http://localhost:3100/api/protobuf',
  params: {},
  headers: { 'Content-Type': 'application/octet-stream' },
  withCredentials: true,
  crossDomain: true,
  extra_config: {
    ignore_response_content_length: false,
    user_agent: '',
    request_timeout: '',
    ssl: { disable_ssl_validation: false },
    proxy: {},
    origin: '',
    follow_location: true,
    max_redirects: -1
  },
  data: Uint8Array [
    10,   6, 112, 114,
    97, 110,  97, 118
  ]
}
@JCMais
Copy link
Owner

JCMais commented Jun 15, 2020

Hi, you want to use the READFUNCTION function, an example with streams is available at https://github.com/JCMais/node-libcurl/blob/6695c91987c6d8a0782cb5a769fb208bf00fea8d/examples/08-file-upload-stream.js

The caveat is that by using this option you cannot use POSTFIELDS or HTTPPOST. So if you also want to send additional data you must build the post payload yourself.

One feature that would allow submitting buffers directly in the POST upload would be to add support for CURLFORM_BUFFERPTR as mentioned in the libcurl docs for curl_formadd (which is the function used internally to add each field): https://curl.haxx.se/libcurl/c/curl_formadd.html

However, currently there is no support for this param, as can be seen here:

CURLFORMcode CurlHttpPost::AddFile(char* fieldName, long fieldNameLength, // NOLINT(runtime/int)
char* fileName) {
return curl_formadd(&this->first, &this->last, CURLFORM_COPYNAME, fieldName, CURLFORM_NAMELENGTH,
fieldNameLength, CURLFORM_FILE, fileName, CURLFORM_END);
}
CURLFORMcode CurlHttpPost::AddFile(char* fieldName, long fieldNameLength, // NOLINT(runtime/int)
char* fileName, char* contentType) {
return curl_formadd(&this->first, &this->last, CURLFORM_COPYNAME, fieldName, CURLFORM_NAMELENGTH,
fieldNameLength, CURLFORM_FILE, fileName, CURLFORM_CONTENTTYPE, contentType,
CURLFORM_END);
}
CURLFORMcode CurlHttpPost::AddFile(char* fieldName, long fieldNameLength, // NOLINT(runtime/int)
char* fileName, char* contentType, char* newFileName) {
return curl_formadd(&this->first, &this->last, CURLFORM_COPYNAME, fieldName, CURLFORM_NAMELENGTH,
fieldNameLength, CURLFORM_FILE, fileName, CURLFORM_CONTENTTYPE, contentType,
CURLFORM_FILENAME, newFileName, CURLFORM_END);
}
CURLFORMcode CurlHttpPost::AddField(char* fieldName, long fieldNameLength, // NOLINT(runtime/int)
char* fieldValue,
long fieldValueLength) { // NOLINT(runtime/int)
return curl_formadd(&this->first, &this->last, CURLFORM_COPYNAME, fieldName, CURLFORM_NAMELENGTH,
fieldNameLength, CURLFORM_COPYCONTENTS, fieldValue, CURLFORM_CONTENTSLENGTH,
fieldValueLength, CURLFORM_END);
}
} // namespace NodeLibcurl

Adding support for that is not something I think I will do, as the HTTPPOST libcurl option was deprecated, instead we should add support for the MIMEPOST option and their related functions: #112

@pranavwani
Copy link
Author

Thanks, @JCMais for put attention on this query. I will try to implement as you suggested.

Specific reason to sending buffer over HTTP API is: request payload is encoded via protobuf. To keep the benefits of protobuf to sending binary data over HTTP API raised the query.

@JCMais
Copy link
Owner

JCMais commented Jun 15, 2020

No problem, please let me know if you have any issues

@JCMais
Copy link
Owner

JCMais commented Jun 17, 2020

@pranavwani I've added an example showing how to send binary data, like in your specific scenario, sending protobuf encoded data: https://github.com/JCMais/node-libcurl/blob/develop/examples/19-binary-data-protobuf.js

@pranavwani
Copy link
Author

Thanks Jonathan for help.

@JCMais JCMais closed this as completed Jun 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants