HTTP POST request with file and string parameter to Node.js - http

I have written a POST route in Express.js: /api/file/upload. This route needs two parameters to work - a "file" parameter with the posted file and an "apiKey" parameter, which is a string. To test it, I am trying to create a successful request in Fiddler2 with the following data:
Headers:
Content-Type: multipart/form-data; boundary=-------------------------acebdf13572468
User-Agent: Fiddler
Host: localhost:8000
Content-Length: 178037
Request Body:
---------------------------acebdf13572468
Content-Disposition: form-data; name="file"; filename="4Byl64P (1).jpg"
Content-Type: image/jpeg
<#INCLUDE *C:\Users\patrick\Pictures\4Byl64P (1).jpg*#>
---------------------------acebdf13572468--
---------------------------acebdf13572468
Content-Disposition: form-data; name="apiKey"
Content-Type: application/json
{
"apiKey": "GKBG-QoNs-f74E-Z8Qn-zozm"
}
---------------------------acebdf13572468--
But when I try to log the parameters in Node.js, I get an empty object for request.body and undefined for request.files.
How do I successfully POST form data to Node.js using Fiddler2?

Your body is malformed (premature ending boundary). It should probably look more like this:
---------------------------acebdf13572468
Content-Disposition: form-data; name="apiKey"
Content-Type: application/json
{
"apiKey": "GKBG-QoNs-f74E-Z8Qn-zozm"
}
---------------------------acebdf13572468
Content-Disposition: form-data; name="file"; filename="4Byl64P (1).jpg"
Content-Type: image/jpeg
<#INCLUDE *C:\Users\patrick\Pictures\4Byl64P (1).jpg*#>
---------------------------acebdf13572468--

Related

I can't add a Header to a specific multipart part in golang

I'm using an API that requires the content-type of a multipart form to be Content-Type: audio/wav but if you add a file with
part, _ := writer.CreateFormFile("audio_file", "test2.wav")
it makes the content-type application/octet-stream
I've tried:
part.Header.Set("Content-Type", "audio/wav")
but Header is undefined.
Here is the curl request data minus the binary that works:
Content-Disposition: form-data; name="audio_file"; filename="test2.wav"
Content-Type: audio/wav
Here is my request minus the binary data that is rejected:
Content-Disposition: form-data; name="audio_file"; filename="test2.wav"
Content-Type: application/octet-stream
Call CreatePart directly instead of the CreateFormFile convenience method. Set the content type in the header used to create the part.
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition",
fmt.Sprintf(`form-data; name="%s"; filename="%s"`, "audio_file", "test2.wav"))
h.Set("Content-Type", "audio/wav")
part, err := writer.CreatePart(h)

Rest Client vs Code extension Problem with Form Data

I passed a form data using the Rest client. But it always shows an empty object in vs code console log.
This is the request.
POST {{Host_LOCAL}}/support/testFormData
Authorization: Bearer {{token}}
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="username"; name="email"
Content-Type: text/plain
{
"username": "John Doe",
"email": "123456"
}
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Where is the problem?
Thanks in advance

Sending simple HTTP Post Request on ESP8266

I need to send simple HTTP POST Request using ESP8266. It's containing data in "form-data".
It should look like this:
POST http://testserver.com
{
"auth_key":"key",
"data":[
{
"key":"temperature",
"value":31.2
},
{
"key":"humidity",
"value":50
}
]
}
For the testing I was using Chrome application - Postman. And the HTTP Request code which I send was looking like this (it was automatically generated):
POST /api/mes HTTP/1.1
Host: testserver.com
Cache-Control: no-cache
Postman-Token: 9b910ed2-afdc-2a11-4963-2f85626cfa4e
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="auth_key"
79bde0ff1efeaee90b1e432c08d324ecfdb532ac42406d7a9a87dd911e95f87e
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="data"
[{"key":"humidity", "value":55}]
------WebKitFormBoundary7MA4YWxkTrZu0gW--
And through Postman everything was okay. So then I sent it through ESP8266:
client.setNoDelay(true);
client.println("POST /api/mes HTTP/1.1");
client.println("Host: testserver.com");
client.println("Cache-Control: no-cache");
client.println("Postman-Token: 9b910ed2-afdc-2a11-4963-2f85626cfa4e");
client.println("Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
client.println("");
client.println("------WebKitFormBoundary7MA4YWxkTrZu0gW");
client.println("Content-Disposition: form-data; name=\"auth_key\"");
client.println("");
client.println("79bde0ff1efeaee90b1e432c08d324ecfdb532ac42406d7a9a87dd911e95f87e");
client.println("------WebKitFormBoundary7MA4YWxkTrZu0gW");
client.println("Content-Disposition: form-data; name=\"data\"");
client.println("");
client.println("[{\"key\":\"humidity\", \"value\":55}]");
client.println("------WebKitFormBoundary7MA4YWxkTrZu0gW--");
But unfortunately server is returning that the request is not correct. What can cause the problem? I'm struggling with it but I don't have any more ideas.

How to write POST HTTR request for Spotify API

I am trying to create a playlist on my Spotify in R via HTTR and the Spotify API. I can't quite figure out how to structure the POST request however.
This is the POST request I am using in R
HeaderValue = "Authorization Code"
n = "application/json"
response = POST(
'https://open.spotify.com/v1/users/(myuserid)/playlists',
accept_json(),
add_headers(Authorization = HeaderValue, "Content-Type" = n),
body = list(name = "New Playlist1", public = "true"),
encode = 'form',
verbose()
)
This is what the request SHOULD look like from the Spotify API website:
POST /v1/users/121616946/playlists HTTP/1.1
Host: api.spotify.com
Content-Length: 40
Accept-Encoding: gzip, deflate, compress
Accept: application/json
User-Agent: Spotify API Console v0.1
Content-Type: application/json
Authorization: "Authorization Code"
{"name": "New Playlist", "public": true}enter code here
This is what I get in the R Console
POST /v1/users/121616946/playlists HTTP/1.1
Host: open.spotify.com
User-Agent: libcurl/7.43.0 r-curl/1.2 httr/1.2.1
Accept-Encoding: gzip, deflate
Accept: application/json
Authorization: "Authorization Code"
Content-Type: application/json
Content-Length: 32
name=New%20Playlist1&public=true
HTTP/1.1 404 Not Found
Server: nginx
Date: Fri, 25 Nov 2016 19:57:41 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Keep-Alive: timeout=10
Vary: Accept-Encoding
Cache-Control: no-cache
Content-Encoding: gzip
Tl;Dr: I think my issue is the body of the POST. I don't think I'm formatting it correctly in the call. I may be missing something else though, but I'm not sure what. The Authorization token is fully authorized to do what I am asking it do as well, so that will not be an issue.

Using AngularJS $http with asp.net webservice, is there a way to set the request headers?

I've just come across a bizarre issue with regards to retrieving data via asp.net webservice.
when using JQuery's ajax method the headers are set correctly and the data is retrieved in JSON successfully.
JSON example:
$.ajax({
type: "GET",
url: "service/TestService.asmx/GetTestData",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: callback,
error: function (err, xhr, res) {
alert(err);
}
});
The Request Headers for the above is the following:
Accept application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Type application/json; charset=utf-8
Host localhost
Referer http://localhost/
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0
X-Requested-With XMLHttpRequest
The Reponse Headers for above is the following:
Cache-Control private, max-age=0
Content-Length 327
Content-Type application/json; charset=utf-8
Date Tue, 29 Oct 2013 17:59:56 GMT
Server Microsoft-IIS/7.5
X-AspNet-Version 4.0.30319
X-Powered-By ASP.NET
this works fine.
But for AngularJS $http method the Request Headers Content-Type value is not set, therefore the Response Headers Content-Type defaults to text/xml; charset=utf-8. Have a look at the example below:
$http({
method : 'GET',
url: 'service/TestService.asmx/GetTestData',
headers: {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Content-Type': 'application/json; charset=utf-8'
}
}).success(callback);
The Request Headers for above is as follows, you will see that Content-Type is missing:
Accept application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Host localhost
Referer http://localhost/ComponentsAndRepos/
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0
therefore the Response Headers for the above is the following:
Cache-Control private, max-age=0
Content-Encoding gzip
Content-Length 341
Content-Type text/xml; charset=utf-8
Date Tue, 29 Oct 2013 17:59:56 GMT
Server Microsoft-IIS/7.5
Vary Accept-Encoding
X-AspNet-Version 4.0.30319
X-Powered-By ASP.NET
therefore this forces the response to return as XML not JSON, is there a way to resolve this?
thank you,
Update
Thanks to Erstad Stephen
This has been resolved by adding data:{} property to $http method.
$http({
method : 'GET',
url: 'service/TestService.asmx/GetTestData',
data: {},
headers: {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Content-Type': 'application/json; charset=utf-8'
}
}).success(callback);
You can handle this a couple of different ways:
You can set the header defaults through the $httpProvider: http://docs.angularjs.org/api/ng.$http#description_setting-http-headers
You can also use the Interceptors in Angular to intercept the idea for $http to modify the config object for all requests: http://docs.angularjs.org/api/ng.$http#description_interceptors
You could also set the config setting like you are above.
The biggest thing is that you maybe misunderstanding how the config works. See this question here: Angular, content type is not being generated correctly when using resource

Resources