Is it possible to PUT a file with Http Utils as multipart form encoded?
This is what I tried:
var response = $"{_baseUrl}{address}".PutBytesToUrl(File.ReadAllBytes(filePath), "image/jpeg", "*/*",
requestFilter: req =>
{
req.Headers["x-aws-acl"] = "private";
req.Headers["content_type"] = "image/jpeg";
req.Headers["X-Shopify-Access-Token"] = _accessToken;
});
The request goes through with 200 but the API (Shopify) doesn't have the image.
I tried running the request in postman and with postman the request works and shopify has the image after.
I used webhook.site to see what the different was in http utils and postman and it seems postman is sending multipart encoded form.
Here are http utils headers being sent that result in no image:
Here are postman headers:
Any way to get http utils to send the image as multipart form data?
To Upload Files as multipart/form-data you would need to use the UploadFile APIs which accepts an overload for specifying which HTTP Method to use, e.g:
var webReq = (HttpWebRequest)WebRequest.Create("http://example.org/upload");
webReq.Accept = MimeTypes.Json;
using var stream = uploadFile.OpenRead();
webReq.UploadFile(stream, uploadFile.Name, MimeTypes.GetMimeType(uploadFile.Name),
method:"PUT");
Related
I'm using Axios npm package in my Node.js application which is a built-in microservices architecture. API-gateway is using Axios to call other services.
According to axios documentation, for POST method, request format is like: axios#post(url[, data[, config]]) . means only one argument data.
Can anyone tell me how I can send req.body & req.files data separately to Axios?
According to answer in Axios community to familiar question this could be achieved with the npm package form-data, which simulates WebAPI FormData on the node side.
This would allow you to send files and form fields to the API and receive it in form of multipart form data.
const axios = require('axios');
const FormData = require('form-data');
const form = new FormData();
// Second argument can take Buffer or Stream (lazily read during the request) too.
// Third argument is filename if you want to simulate a file upload. Otherwise omit.
form.append('field', 'a,b,c', 'blah.csv');
axios.post('http://example.org/endpoint', form, {
headers: form.getHeaders(),
})
.then(result => {
// Handle result…
console.log(result.data);
});
Bundle your data up as an object, and pass that object through to the axios request as a single payload.
On the other side of the API, you can access the object, and do as you wish.
var payload = { property1: 'values', property2: 'values2' }
now you pass payload around
const api = supertest(app)
const newblog=await api.post({
url: '/api/blogs',
headers: {authorization:"bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IjExMTEiLCJpZCI6IjVlZWFjMThkZmM3MDUzNzAwMGRkMjc4NCIsImlhdCI6MTU5MjQ0Mzk4NH0.D4TJ502z-eudWntUaPKneg3kaoS6iSfc2CVZdl7OcRs"}
}).send({title: 'g',author: "b",url: "f",likes: 0}).expect(201)
it causes error
I can only send header and succees in Postman, but not in code. How to write correctly?
supertest package explained, there is about setting a header and whatever
https://github.com/visionmedia/supertest/issues/398
I would like to POST two data in multipart / form-data format using Node-RED.
(One for text data, one for voice data)
I set the function node and http request node as follows, but it does not seem to be POST.
I think that it is necessary to create a multi-part body and assign it to msg.body, but I do not know how to create a multi-part body of the voice data.
I do not know how to solve it, so someone would like to tell me.
function node
var request = global.get('requestModule');
var fs = global.get('fsModule');
msg.body = {};
msg.body = {
'apikey' : "**********",
'wav' : fs.createReadStream('/tmp/testtest.wav')
};
msg.headers = {};
msg.headers['Content-type'] = 'multipart/form-data';
return msg
http request(Property)
method ⇒ POST
URL ⇒ https://xxxxyyyzzz/
SSL/TLS ⇒ No
Basic ⇒ No
Output ⇒ JSON
The http request Node-Red core node support multipart/form-data POST out of the box.
Add a function node before your http request with this Function :
msg.headers = {};
msg.headers['Content-Type'] = 'multipart/form-data';
msg.headers['Accept'] = 'application/json';
msg.payload = {
'apikey': msg.apiKey,
'wav': {
value: msg.payload.invoice.file,
options: {
filename: 'testtest.wav',
contentType: 'audio/wav', // This is optionnal
}
}
}
return msg;
The http request node use Request nodejs library under the hood, and this one use form-data library for handling multipart, so all the options supported by these works.
The source code of the relevant part of http request handling multipart.
I'm using Axios on the client side to send HTTP requests to a remote Node.js server. How might I go about sending files in the request body using Axios? I also have to send other information in the request body - sending just the files to the server will not suffice. How might I go about doing this? I am open to using a different HTTP client as well.
Use a FormData instance. On node you can use the form-data npm package. Then you just send that FormData instance as the data in an axios request.
var formData = new FormData();
formData.append("username", "Groucho");
formData.append("accountnum", 123456); // number 123456 is immediately converted to a string "123456"
// HTML file input, chosen by user
formData.append("userfile", fileInputElement.files[0]);
// JavaScript file-like object
var content = '<a id="a"><b id="b">hey!</b></a>'; // the body of the new file...
var blob = new Blob([content], { type: "text/xml"});
formData.append("webmasterfile", blob);
axios.post("http://foo.com/submitform.php", formData);
I am using httpSync module to fetch data from the server.
I have sample as below
var httpsync = require('httpsync');
var req = httpsync.request({
url: url,
method: "POST"
});
Note:
I am using httpSync because i want the request to behave synchronous.
I cannot add original code
How can i send the form-data to the server using httpSync
I had tried the following but it is not working
var req = httpsync.request({
url: url,
method: "POST"
form:{a:1,b:1}
});
Any suggestion will be grateful
httpsync module has an example for this.
Check this link
https://github.com/fengmk2/node-curl/blob/master/example/upload_file.js