Buddha
Buddha

Reputation: 4476

Unable to post json using c# to google cloud endpoint

I have created an endpoint to an entity and deployed to google cloud app engine.

https://alert-streamer-857.appspot.com/_ah/api/error/v1/error

If I POST the following JSON using Advanced REST Client in chrome, the item is successfully getting added to datastore.

{
    ipAddress: "123.456.789.098",
    host: "jbuddha-lap",
    user: "buddha",
    message: "testing from c#"
}

I'm getting following response

{
 "id": "5642554087309312",
 "syncTime": "2015-02-16T06:52:10.347Z",
 "user": "buddha",
 "host": "jbuddha-lap",
 "ipAddress": "123.456.789.098",
 "message": "testing from c#",
 "kind": "error#resourcesItem",
 "etag": "\"7D_54po0JQelJxcYULKayrO-_rE/GG6ep9m3yZRlAja6F1zqro8PO20\""
}

This is as expected. However the problem I'm facing is I'm trying to send the exact same json from a c# program, it is not getting added successfully. Following is the c# program that is sending this info.

string json = "{ ipAddress: \"123.456.789.098\",host: \"jbuddha-lap\",user: \"buddha\",message: \"testing from c#\"}";
string result = "";
using (var client = new WebClient())
{
    result = client.UploadString("https://alert-streamer-857.appspot.com/_ah/api/error/v1/error", "POST", json);
}
Console.WriteLine(result);

Following is the JSON response. The values for id and syncTime are autogenerated, that is the reason they appear in both the responses. But as you may have seen insertion is not happening correctly.

{
 "id": "5086441721823232",
 "syncTime": "2015-02-16T06:57:28.249Z",
 "kind": "error#resourcesItem",
 "etag": "\"7D_54po0JQelJxcYULKayrO-_rE/Npk3iyaHaGMHYmOeFtt5sPUK1yU\""
}

Can anyone tell me where I'm doing it wrong?

Upvotes: 1

Views: 270

Answers (1)

Parthasarathy
Parthasarathy

Reputation: 2818

You have to set the content type as "application/json" like below.

client.Headers.Add(HttpRequestHeader.ContentType, "application/json");

Upvotes: 2

Related Questions