[SalesForce] Submit data using post by attaching data from request body

How to submit data using post and attach to request body?

Salesforce code:

    string jsonstring = {"User": "testuser"}
    HttpRequest req = new HttpRequest();
    req.setEndpoint('http://testsite.com/api/user');
    req.setMethod('POST');
    req.setHeader('Content-Type', 'application/json');
    req.setHeader('Content-Length', '300000'); 
    req.SetBody(jsonstring);
    Http http = new Http(); 
    HTTPResponse res = http.send(req);

C# code:

public string PostUser([frombody] string user)
{
  //process the data attach from the request body.
}

I omitted the code block inside the sample code in c#, but it save the user from the database. By sending the post request it return null. I assumed that the data from request body is not being received from my c# post method. Am I missing something?

Best Answer

I see you have declared variable as jsonstring but sending json, Do you have some other variable by name json ? You can try to add debug statement after setbody method if data is correctly sent and also use Network tool of browser like Google chrome to see what request was sent. Atleast we can debug if Postbody is sent or not from code.

Related Topic