POST Request through WebProxy with JSON data
-
30-09-2020 - |
Question
I want to make a POST request with JSON data to a REST API through the WebProxy. Therefor I make a request like:
$.ajax({
url: "../_api/SP.WebProxy.invoke",
type: "POST",
data: JSON.stringify(
{
"requestInfo": {
"__metadata": { "type": "SP.WebRequestInfo" },
"Url": someURL,
"Method": "POST",
"Headers": {
"results": [{
"__metadata": { "type": "SP.KeyValue" },
"Key": "Accept",
"Value": "application/json;odata=verbose",
"ValueType": "Edm.String"
}]
},
"Body": json
}
}),
headers: {
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
}
});
Now my Question is where do I have do put my JSON data? The data element of the request seems to be for the request to the webproxy. So I guess I need to send the data within the "inner" request but with the body element it doesn't work.
Solution
Add Content-Type
with application/json
to the result-array of headers (same as with Accept
), like this:
results: [{
__metadata: {
type: "SP.KeyValue"
},
Key: "Accept",
Value: "application/json",
ValueType: "Edm.String"
}, {
__metadata: {
type: "SP.KeyValue"
},
Key: "Content-Type",
Value: "application/json",
ValueType: "Edm.String"
}]
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange