1

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.

  • Have you tried adding Content-Type with application/json to the result-array of headers (same as with Accept)? – eirikb Nov 19 '14 at 8:51
  • Yes I did, but then I get a bad request error saying that Content-Type and Accept are not support by SP.KeyValue – Mars Nov 19 '14 at 9:09
  • Depends on how you structured it, this is how it should look like: gist.github.com/eirikb/bf25e0d1094d9ff52326 – eirikb Nov 19 '14 at 9:13
  • Holy, it did? That is cool, I honestly didn't expect it to. Let me post an answer for you go give me internet points – eirikb Nov 19 '14 at 9:19
0

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"
}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.