I was using bash to do a task. And had some mess while trying to parse the response. Now I am using nodejs for the task. But I get following error:
"httpStatus" : 415,
"messages" : [ {
"errorCode" : "305",
"message" : "Unsupported media type 'application/x-www-form-urlencoded'"
} ]
This used to be my curl request in bash file:
curl --include\
--request POST \
--user "$USERNAME:$PASSWORD" \
--header "Content-Type: application/vnd.profitbricks.resource+json" \
--data-binary "{
\"properties\": {
\"name\": \"$servername\",
\"ram\": $RAM,
\"cores\": $CORES
}
}" \
https://api.profitbricks.com/rest/datacenters/$ID/servers ;
This is my current request:
var request = require('request');
var reqoptions = {
method: 'POST',
uri: 'https://api.profitbricks.com/rest/datacenters/'+options.vdcID+'/servers',
form:{
"properties":{
"cores": options.cores,
"ram": options.ramsize,
"name": options.servername
}
},
headers: {
'Authorization': 'Basic ' + new Buffer(options.user+':'+options.password).toString('base64'),
'Content-Type': 'application/vnd.profitbricks.resource+json'
}
};
request(reqoptions, function(err, res, body){});
form option changing content-type to form-urlencoded
you shouldn't use form in request options
send a binary data like here nodejs/express and binary data in POST
so use body: myBuffer instead of form: {...}
The problem was serialization. I stringified the object. Now it works.
var request = require('request');
var body = {
"properties":{
"cores": options.cores,
"ram": options.ramsize,
"name": options.servername
}
}
var reqoptions = {
method: 'POST',
uri: 'https://api.profitbricks.com/rest/datacenters/'+options.vdcID+'/servers',
body: JSON.stringify(body),
headers: {
'Authorization': 'Basic ' + new Buffer(options.user+':'+options.password).toString('base64'),
'Content-Type': 'application/vnd.profitbricks.resource+json'
}
};
request(reqoptions, function(err, res, body){});
This did the trick.
Related
I wanna purge an image from the imgix cache, they have a API for that (https://docs.imgix.com/setup/purging-images)
I use axios to make the request from my nodejs backend:
const imgixKey = functions.config().imgix.key;
const headers = {
Authorization: `Bearer ${imgixKey}`,
'Content-Type': 'application/json',
};
const data = JSON.stringify({
type: 'purges',
attributes: {
url: href,
source_id: 'SOsourceId',
},
});
try {
await axios.post('https://api.imgix.com/api/v1/purge', data, { headers });
functions.logger.log('Purged Image successfully' + href);
} catch (e) {
functions.logger.error('Error removing image from cache ' + href + ' -> ' + e);
}
Error removing image from cache {stackoverflowimgpath.png} -> Error: Request failed with status code 400
The problem is other than the status code 400 there is nothing else specified, I checked the image paths and tried different Content-Type headers like in their example but that also did not work.
Since I cannot get any more information out of the response I ask for help here since I do not know how to continue.
Checking imgix documentation, the body of your request should have a data property:
curl -X POST 'https://api.imgix.com/api/v1/purge' \
--header 'Authorization: Bearer <api-key>' \
--header 'Content-Type: application/vnd.api+json' \
--data-raw '{ "data": { "attributes": { "url": "<url-to-purge>" }, "type": "purges" } }'
So, just modify your message to (adding the data property to your json object):
const data = JSON.stringify({
data: {
type: 'purges',
attributes: {
url: href,
source_id: 'SOsourceId',
},
}
});
I am trying to make a http post request towards an external API from my NodeJS application. I tried two different approaches I found on the web, however both failed with different issues. The working curl command that results in the correct request looks like this:
curl -d ' { "auth_token":"XXXXXXXXX",
"hrows": [ {"cols": [ {"value":"Name 0"}, {"value":"Value 0"} ] } ],
"rows": [ {"cols": [ {"value":"Name 1"}, {"value":"Value 1"} ] },
{"cols": [ {"value":"Name 2"}, {"value":"Value 2"} ] },
{"cols": [ {"value":"Name 3"}, {"value":"Value 3"} ] },
{"cols": [ {"value":"Name 4"}, {"value":"Value 4"} ] } ]
}' http://example.com:3030/widgets/alarms
1) Trying to make a request with the request library. This doesn't throw an error in the application, but I get an empty request on the API Server (Yes, the content I want to send is a string)
var request = require('request');
var test = "{\"auth_token\":\"XXXXXXXXXX\", \"hrows\": [ {\"cols\": [{\"value\":\"Loc Nr.\"},{\"value\":\"Address\"},{\"value\":\"Status\"}] } ], \"rows\": [ {\"cols\": [ {\"value\":\"Name 1\"}, {\"value\":\"Value 1\"} ] }, {\"cols\": [ {\"value\":\"Name 2\"}, {\"value\":\"Value 2\"} ] } ]}";
var wid = "alarms";
postRequest(wid,test);
function postRequest(widget, content) {
var headers = {
'User-Agent': 'Super Agent/0.0.1',
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(content)
}
var options = {
traditional: true,
url: 'http://example.com:3030/widgets/'+widget,
method: 'POST',
headers: headers,
data: content,
contentType : "application/x-www-form-urlencoded"
}
console.log(options);
request.post(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
})
}
When I do a tcpdump on the API server, I see that the packet received looks similar to the one received when executing the curl command, except for the field http.content_length_header being set to 0 (And the conrent itself is missing). When I check the debug output of the options variable, it sill looks ok:
{ traditional: true,
url: 'http://example.com:3030/widgets/alarms',
method: 'POST',
headers:
{ 'User-Agent': 'Super Agent/0.0.1',
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': 243 },
data:
'{"auth_token":"XXXXXXXXX", "hrows": [ {"cols": [{"value":"Loc Nr."},{"value":"Address"},{"value":"Status"}] } ], "rows": [ {"cols": [ {"value":"Name 1"}, {"value":"Value 1"} ] },\t{"cols": [ {"value":"Name 2"}, {"value":"Value 2"} ] } ]}',
contentType: 'application/x-www-form-urlencoded' }
As you can see content length as well as data are there but not in the packet received (See attached Screenshots, one from the curl command and one from the NodeJS request, both captured on the API server)
Any idea why this is happening?
2) The second way I tried was from a Stackoverflow Post I found on how to post a string. However that one fails with
Error: connect ECONNREFUSED 127.0.0.1:80
though I don't understand why it wants to bind to port 80 on my localhost, however method #1 would anyway be the preferred one, this is just for completeness (And maybe someone knows a smart answer to this as well
var querystring = require('querystring');
var http = require('http');
var fs = require('fs');
var test = "{\"auth_token\":\"XXXXXXXX\", \"hrows\": [ {\"cols\": [{\"value\":\"Loc Nr.\"},{\"value\":\"Address\"},{\"value\":\"Status\"}] } ], \"rows\": [ {\"cols\": [ {\"value\":\"Name 1\"}, {\"value\":\"Value 1\"} ] }, {\"cols\": [ {\"value\":\"Name 2\"}, {\"value\":\"Value 2\"} ] } ]}";
var wid = "alarms";
postRequest(wid,test);
function postRequest(widget, content) {
var headers = {
'User-Agent': 'Super Agent/0.0.1',
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(content)
}
var options = {
url: 'http://example.com:3030/widgets/'+widget,
method: 'POST',
headers: headers,
}
var pdata = querystring.stringify(content);
console.log(pdata)
console.log(Object.prototype.toString.call(pdata));
var post_req = http.request(options, function() {
});
// post the data
post_req.write(pdata);
post_req.end();
}
Thanks for any useful hints (Or a hint how to trigger a dashing job using the dashing API but as far as I see that's not possible so I have to stick with these API calls :) )
It goes wrong in
var options = {
traditional: true,
url: 'http://example.com:3030/widgets/'+widget,
method: 'POST',
headers: headers,
data: content,
contentType : "application/x-www-form-urlencoded"
}
data isn't a valid key on request options, it's either body or form. That would
explain why no body data is being sent in the request.
In your case you want form
form - when passed an object or a querystring, this sets body to a querystring representation of value, and adds Content-type:
application/x-www-form-urlencoded header. When passed no options, a
FormData instance is returned (and is piped to request). See "Forms"
section above.
var options = {
traditional: true,
url: 'http://example.com:3030/widgets/'+widget,
method: 'POST',
headers: headers,
form: content,
contentType : "application/x-www-form-urlencoded"
}
see https://www.npmjs.com/package/request#requestoptions-callback for more details.
Here is an example post that I want to do in my Nodejs server to get ClientID and secret.
Request example:
curl --request POST \
--url https://api.opskins.com/IOAuth/CreateClient/v1/ \
--header 'authorization: Basic {{AUTH_HASH}}' \
--data name=TestApp2 \
--data redirect_uri=http://localhost:1234
Response returns JSON structured like this:
{
"status": 1,
"time": 1535408581,
"response": {
"secret": "$nGwYVda##PErKAUpG#kHQ&YA1L)A*X1",
"client": {
"client_id": "ff371b045307",
"name": "TestApp2",
"redirect_uri": "http://localhost:1234",
"time_created": 1535407757,
"has_secret": true
}
}
I am trying with request :
const request = require('request');
var headers = {
'authorization': 'Basic ***my-api-key****'
};
var dataString = 'name=TestApp2&redirect_uri=http://localhost:5000';
var options = {
url: 'https://api.opskins.com/IOAuth/CreateClient/v1/',
method: 'POST',
headers: headers,
body: dataString
};
function callback(error, response, body) {
console.log(body);
}
request(options, callback);
but getting error output like this :
{"status":401,"time":1540115259,"message":"API Key Required"}
I have been trying different codes and middlewares but couldn't make it. Also my test works perfect on Postman. I need help to post that and get my client_id and secret.
I found a way. It was really tricky for me. I needed to use my API Key and also client id.
Bellow codes worked. Thank you very much vitomadio !
I used reqclient from npm.
here is my code
var client = new RequestClient({
baseUrl:"https://api.opskins.com/IOAuth/CreateClient/",
debugRequest:true, debugResponse:true,
auth: {
user:"***My_apikey****",
}
});
var resp = client.post("v1/", {"name": "testApp2", "redirect_uri":"http://localhost:5000/"},{headers: {"authorization":"Basic **my_clientID"}}).then(response => {
// console.log(response);
});
also I am really curies and would like to know how can I do that with request.
I have this curl request working.
curl -v "https://developer.api.autodesk.com/oss/v2/buckets/:bucketName/objects/"
-X "PUT" -H "Authorization: Bearer tokenGoesHere"
-H "Content-Type: application/octet-stream" -T "forupload.rvt"
How can I write this in node with npm request module.
I tried the following with 'request' and 'fs'.
I get back "Token is not provided in the request".
function uploadFile(bucketData){
var uri = 'https://developer.api.autodesk.com/oss/v2/buckets/' + bucketData['bucketKey'] + '/objects/'
var authorizationHeader = ' Bearer ' + bucketData['token'] // this works in other post/get requests
var contentTypeHeader = 'application/octet-stream'
var streamTarget = 'C:\\Users\\architech\\Desktop\\Forge Node\\Test.rvt';
console.log(uri)
console.log(authorizationHeader)
console.log(contentTypeHeader)
console.log(streamTarget)
// console output:
// https://developer.api.autodesk.com/oss/v2/buckets/bucketpqglrzt/objects/
// Bearer ....token....
// application/octet-stream
// C:\Users\architech\Desktop\Forge Node\Test.rvt
request.put(
{
url: uri,
// preambleCRLF: true,
// postambleCRLF: true,
multipart:
[
{
'Authorization': authorizationHeader,
'Content-Type': contentTypeHeader,
body: fs.createReadStream(streamTarget)
},
]
},
function(error, response, body){
if(!error){
console.log(body);
}else{
console.log(error);
}
})
}
After trying several approaches, while I couldn't reproduce your specific problem, the trouble I had was with the binary attachment loading properly. Because createReadStream() runs asynchronously, it doesn't really seem to work the way the request docs say it should when added to the multipart or formData keys. Not sure why this is?
I got it working first using http://requestb.in - comparing the curl request to the same request constructed with Node. Here is the final, working version:
var request = require('request')
fs = require('fs')
var options = {
uri: 'https://developer.api.autodesk.com/oss/v2/buckets/<yourBucket>/objects/<yourFile.ext>',
headers: {
'Content-Type': 'application/octet-stream',
'Authorization': 'Bearer <token>'
}
}
fs.createReadStream(__dirname + '/<fileName.ext>').pipe(request.put(options, function(err, response, body) {
console.log(body)
/*
{
"bucketKey" : "< yourBucket>",
"objectId" : "urn:adsk.objects:os.object:brandontestbucket2/skyscpr1.3ds",
"objectKey" : "<fileName.ext>",
"sha1" : "...redacted...",
"size" : 43791,
"contentType" : "application/octet-stream",
"location" : "https://developer.api.autodesk.com/oss/v2/buckets/< yourBucket>/objects/<fileName.ext>"
}
*/
}))
I need to set the password for Neo4j and can do it from the command line like this:
curl -H "Content-Type: application/json" \
-H "Authorization: Basic `echo -n 'neo4j:neo4j' | base64`" \
-X POST -d '{"password":"nopass"}' \
http://localhost:7474/user/neo4j/password
but I'm now trying to do it in node.js like this:
var f = require('node-fetch');
var url = 'http://neo4j:n0p4ss#localhost:7474/user/neo4j/password';
var auth = new Buffer('neo4j:neo4j').toString('base64');
f(url, {
method: 'POST',
body: {'password': 'nopass'},
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + auth
}
})
.then(res => res.text())
.then(txt => { console.log(txt); });
however, what I get back is:
{
"errors" : [ {
"code" : "Neo.ClientError.Request.InvalidFormat",
"message" : "Required parameter 'password' is missing."
} ]
}
which to me means the body: {...} isn't getting sent correctly. can anyone see what's wrong with my code?
ah. figured it out. it needs:
body: JSON.stringify({'password': 'nopass'}),