Posting JSON object through WinJS.XHR - xmlhttprequest

I'm having abit of problems posting data from WinJS.xhr to a PHP script.
"obj" is a stringified JSON object
WinJS.xhr({
type: "POST",
url: dataUrl,
headers: { "Content-type": "application/x-www-form-urlencoded" },
data: obj,
})
However the $_POST variable is always empty.
I've tried changing content-types, and escaping the object but no luck :(

Your content-type when posting json should typically be application/json
Secondly make sure you 'stringify' your json object.
Based on: Post JSON data to web services in Windows 8
WinJS.xhr({
type: "post",
url: dataUrl,
headers: { "Content-type": "application/json" },
data: JSON.stringify(obj)
})

Figured out a soloution.
Incase anyone has the same problem i got it working by removing headers from the xhr, and getting the post data # server side with this code:
$data = file_get_contents('php://input');
$data = (array) json_decode($data);

Related

Request body missing from POST requests made using Postman scripting

I am trying to run a pre-request script for authenticating all my requests to the Spotify API. It works via the Postman GUI, but when I try to make the request via scripting, it fails because there is no body. Here is my code
const postRequest = {
url: pm.environment.get("spotifyAuthApi"),
method: "POST",
header: {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "*/*",
"Content-Length": 29,
"Authorization": "Basic " + btoa(pm.environment.get("client_id") + pm.environment.get("client_secret"))
},
body: {
grant_type: "client_credentials",
}
}
I get a
error: "unsupported_grant_type"
error_description: "grant_type parameter is missing"
And when I examine the request via the postman console, there is no request body, even though the request was built exactly like my fetch token request that does the same thing successfully in postman, only via the GUI instead of via a pre-request script. I have searched far and wide about this issue and tried multiple variations of the body object but to no avail. Either the body object doesn't generate my desired field, or it doesn't get created at all.
Mode might be missing in the body object. Try this:
body: {
mode: 'raw',
raw: JSON.stringify({ grant_type: 'client_credentials' })
}
More details might be found in Postman docs for RequestBody.
If you're using urlencoded, the body of the request would be structured like this:
body: {
mode: 'urlencoded',
urlencoded: [
{ key: 'grant_type', value: 'client_credentials'}
]
}

Bandcamp api: What POST info do you send, when querying the my_bands endpoint?

https://bandcamp.com/developer/account#my_bands
It doesnt say what youre sposta send, as POST, and if you send without a POST or empty array, you get a 'must be POST' error. Their support isnt helping.
I have been able to use their other endpoints, so I know I've got the auth correct.
I send an empty payload to https://bandcamp.com/api/account/1/my_bands, like:
var parameters = {
headers: { Authorization: 'Bearer '+access_token },
method: 'post',
payload: '',
muteHttpExceptions: true // we want to handle exceptions gracefully
};

Github API v3, post issues / authentication

I am working on a project making a Kanban board using the Github API v3.
I have no problem with get methods, but when it comes to post methods i get a 404 response, and from what i read in the documentation, this seems to be a authentication error.
I am using personal token for authentication, and have successfully posted through postman, but when i try to post through my own application i get the error.
Link to project if anyone's interested : https://github.com/ericyounger/Kanban-Electron
Below is the code used for posting to github.
Might there be a problem with my code below? Or might it be settings in relation with the token?
postIssue(json){
let packed = this.packPost(json);
return Axios.post(`https://api.github.com/repos/${this.user}/${this.repo}/issues`, packed);
}
packPost(json) {
return {
method: "POST",
headers: {
"Authorization": `token ${this.tokenAuth}`,
"Content-Type": "application/json"
},
body: JSON.stringify({title: json.title})
};
}
This is what i receive:
{message: "Not Found", documentation_url: "https://developer.github.com/v3/issues/#create-an-issue"}
message: "Not Found"
documentation_url: "https://developer.github.com/v3/issues/#create-an-issue"
Console log error message
Without seeing any detailed logs, my first attempt would be to set body to not send the string representation of the body
body: {title: json.title}
This did the trick :)
postIssue(json){
const headers = {
'Content-Type': 'application/json',
'Accept': 'application/vnd.github.v3.raw',
"Authorization": `token ${this.tokenAuth}`,
};
return Axios.post(`https://api.github.com/repos/${this.user}/${this.repo}/issues`, json , {headers: headers});
}

Server response "unsupported media type" when I send JSON format data to Asp.net Core 3.0

Visual Studio 2019
Asp.Net Core 3.0
A. Unsupported media type code
$.ajax({
url: this.url,
data: jsonDataParameter,
cache: false,
type: "Post",
dataType: 'JSON',
contentType: "application/json",
success: function (data) {}
});
B. Successful request code
$.ajax({
url: this.url,
data: JSON.stringify(jsonDataParameter),
cache: false,
type: "Post",
dataType: 'JSON',
contentType: "application/json",
success: function (data) {}
});
Here is my questions:
Is this features or Bugs?
If it is features, why?
Thank you ahead.
contentType is the type of data you're sending, and application/json; charset=utf-8 is a common one to send json data.
In your case, data {a:1,b:2} is only a Javascript object so you need to use the JSON.stringify() method to convert a JavaScript object or value to a JSON string.
Since your content type is application/json;, you need to use [FromBody] and receive the data as an object based on your situation.

Retrieve JSON Request Payload in WCF Service

I have an IErrorHandler set up for funneling all wcf errors through log4net. I'd like to get the json payload data from the request before logging it to the server, but I can't seen to find it in System.Web.Context.Current.Request. I expected it to be in the InputStream, but that's empty.
I'm currently using jquery to do an AJAX post with the json passed in as data.
$.ajax({
url: 'http://test.com/myservice/service.svc',
data: JSON.stringifyWcf({"id":1, "description":"thing"}),
type: 'POST',
processData: true,
cache: false,
contentType: 'application/json; charset=utf-8',
timeout: 5000,
dataType: 'json',
success: function (result) {
//do stuff
}
});
Where I would like to get the payload {"id":1, "description":"thing"}
I guess that the input stream is empty because it has already been consumed. You'll need to hook into the system before it reads the input stream to save it for later I believe. See Request.InputStream is empty when service call is made
How about: OperationContext.Current.RequestContext.RequestMessage?