How can I execute this cURL shell command curl --data "{\"obj\" : \"1234556\"}" --digest "https://USERNAME:PASSWORD#www.someurl.com/rest-api/v0/objectpost" that correctly returns expected values using node's request package?
I tried with those post options but got no success:
var request = require('request');
var body = {"obj" : "1234556"};
var post_options = {
url: url,
method: 'POST',
auth: {
'user': 'USERNAME',
'pass': 'PASSWORD',
'sendImmediately': false
},
headers: {
'Content-Type': 'text/json',
'Content-Length': JSON.stringify(body).length,
'Accept': "text/json",
'Cache-Control': "no-cache",
'Pragma': "no-cache"
},
timeout: 4500000,
body: JSON.stringify(body)
}
request(post_options, callback);
This way the body is not parsed (got something like missing required parameter: "obj"), and I can't understand if it's a matter of encoding or just passing it in the wrong place (i.e. should not be the body). Any suggestion?
By default, cURL will send a Content-Type: application/x-www-form-urlencoded unless you use -F (which changes it to Content-Type: multipart/form-data) for your fields or explicitly override the header (e.g. -H 'Content-Type: application/json'). However, the data being sent by your cURL example seems to be JSON. So the server will get confused and won't correctly find the data it's expecting.
So the solution is one of two options:
Try application/json as a Content-Type in your code instead of text/json.
Actually use urlencoded formatted data instead of JSON by using the form property. request will take that form object and do all the conversions and setting of headers, etc. for you. For example:
var post_options = {
url: url,
method: 'POST',
auth: {
user: 'USERNAME',
pass: 'PASSWORD',
sendImmediately: false
},
timeout: 4500000,
form: body
};
Related
I'm currently trying to implement Polar accesslink API on an app, but when trying to POST a new user I still get this error:
url: 'https://www.polaraccesslink.com/v3/users/',
status: 400,
statusText: 'Bad Request',
headers: Headers { [Symbol(map)]: [Object: null prototype] },
counter: 0
I already have the authorization token, which I know expires every 10min, and I'm using the service through a function that takes the token and the userID as parameters. This is my code:
postUser(memberId: string, token: string) {
const inputBody = { 'member-id': memberId };
const headers = {
'Content-Type': 'application/xml', 'Accept': 'application/json', 'Authorization': 'Bearer ' + token
};
const options = {
method: "POST",
headers: headers,
body: JSON.stringify(inputBody)
}
return new Promise<object>((resolve, reject) => {
fetch('https://www.polaraccesslink.com/v3/users', options)
.then(function(res: any) {
console.log(res)
return res.json();
}).then(function(body: any) {
console.log(body);
});
})
}
I'm implementing it the same way as it is specified in https://www.polar.com/accesslink-api/?javascript--nodejs#users but really don't know what might I be doing wrong. Thanks for helping me!.
I don't have experience with this specific API but i can see you send in the header Content-Type the value application/xml but the request body is JSON formatted.
Try send application/json in that header.
The Content-Type header is used in HTTP to specify the body mime type.
more info in: Content-Type HTTP Header - MDN
I also see this is the exact code in the sample but notice they have 2 sample requests and 2 sample results, one in XML and one in JSON each.
Any ideea why this POST gives 'invalid_Request'?
curl --location --request POST 'https://polarremote.com/v2/oauth2/token?grant_type=authorization_code&code=1f9edc0c5e60a0bab4fd3f1f00571a58' --header 'Authorization: Basic ... DA4OS05ZDc2LTJlNTQwZjFkZTc5ZA==' --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: application/json'
I'm using axios to perform get and post requests to an external api,
i have finally succeed to achieve get request (problem with the ssl certificate, i avoid it by adding this :
httpsAgent: new https.Agent({ rejectUnauthorized: false }),
now i would like to post the api,
to get the request working in postman, i put in
headers content-type : application/json
and in the body : {}
like here
when trying with a google chrome extention, to make it work, i put nothing in the headers but in params, i select customer : application/json and i put inside this {} instead of the default choice which is x-www-form-urlencoded;charset=UTF-8
chrome extention
in my javascript app i tried this
var url = https://10.11.31.100:9440/api/nutanix/v3/images/list;
axios({
method:'post',
httpsAgent: new https.Agent({ rejectUnauthorized: false }),
url,
auth: {
username: '******',
password: '********'
},
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
},
params: {},
data: {}
})
.then(function (response) {
res.send(JSON.stringify(response.data));
console.log(response);
})
.catch(function (error) {
console.log(error);
});
I get this problem :
TypeError : UTF-8 is not a function
Specifically with regards to the Nutanix v3 REST API - this is probably because the POST request above doesn't have an appropriate JSON payload i.e. the "data" parameter is empty.
When sending Nutanix v3 API POST requests, specifically to "list" entities, you'll need to specify the "kind" of entity being listed. In your example, the JSON payload below will work.
{"kind":"image"}
See here: https://nutanix.dev/reference/prism_central/v3/api/images/postimageslist
HTH. :)
I am somewhat new to Nodejs. I am working on a pair of Microservices, and I need one to post data to the other. I am using the request-promise-native library.
My code is to make the call is like this:
const options = {
method: 'POST',
uri: url,
formData: {
command: command,
version: version,
session_id: sid,
aicc_data: data
},
headers: {
'content-type' : 'application/x-www-form-urlencoded'
}
}
rp(options)
However, when I inspect the request as it come in to the other server, the header I have specified does not appear.
headers: { 'content-type': 'multipart/form-data; boundary=--------------------------395968157759002211606136',
host: 'localhost:9000',
'content-length': '513',
connection: 'close' }
What am I doing wrong?
options includes a formData object which enforces multipart/form-data.
You should add the form object instead when you want to use application/x-www-form-urlencoded.
I'm doing my request like that:
var options = {
method: 'GET',
rejectUnauthorized: false,
url: "https://history.ripple.com/v1/accounts/r4cvz8ZLz9xBXWKkQMWpEaeEshQeG2sFxh/transactions?type=Payment&result=tesSUCCESS&limit=1000",
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
}
};
request(options, callback);
It's not working I get an error 500.
But if I set only one parameters to the url, like that:
https://history.ripple.com/v1/accounts/r4cvz8ZLz9xBXWKkQMWpEaeEshQeG2sFxh/transactions?type=Payment
then is works, so I assume the problem is to set the parameters and I don't know how to do. Any clue ?
Thanks a lot !
Ok I found the answer myself it was pretty simple.
You just have to put the query parameters in the options like that:
qs: {type: "Payment", result:"tesSUCCESS", limit:1000},
i'm trying to make a $http.post like this:
$http({
method: 'POST',
url: '//192.168.2.1:3000/auth/signup',
data: $scope.credentials,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
when:
$scope.credentials = {email: 'some_email', password: 'some_password'}
and in Node, I'm logging:
console.log(req.body)
and I get:
{ '{"email":"some_email","password":"some_password"}': '' }
i don't understand why this is happening.
You're assuming angularjs is automatically serializing form data for you.
You need to serialize your form data yourself when dealing with content types that are not json.
For instance, when the Content-Type equals 'application/x-www-form-urlencoded' , given an object {foo:"bar",bar:"baz"} , it needs to be serialized as foo=bar&bar=baz