0

I am doing a HTTP call like so:

[HttpGet]
public HttpResponseMessage updateRegistrant(string token, string registrantId, string firstname, string lastname, string postalCode, string phoneNumber, string city, string email)
{
    using (HttpClient httpClient = new HttpClient())
    {
        httpClient.BaseAddress = new Uri("https://api.example.com/v1/registrants/" + registrantId + "/");
        httpClient.DefaultRequestHeaders.Accept.Clear();
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, "person/contact-information");

        request.Content = new StringContent("{\"firstName\":\"" + firstname + "\", \"lastName\":\"" + lastname + "\", \"phones\":[{\"phone\":\"" + phoneNumber + "\", \"type\":\"Home\", \"primary\":true}], \"emails\":[{\"email\":\"" + email + "\", \"type\":\"Personal\", \"primary\":true}], \"addresses\":[{\"city\":\"" + city + "\", \"zipCode\":\"" + postalCode + "\"}]}", Encoding.UTF8, "application/json");

        //request.Content = new StringContent("{\"firstName\":\"" + firstname + "\", \"lastName\":\"" + lastname + "\"}", Encoding.UTF8, "application/json");

        HttpResponseMessage response = httpClient.SendAsync(request).Result;

        return response;
    }
}

Now when I run this method, I get 409 Error call, however if I comment out the first request.Content and uncomment the second request.Content it works, I get response code of 200.

I would assume that these are causing the 409 error:

\"phones\":[{\"phone\":\"" + phoneNumber + "\", \"type\":\"Home\", \"primary\":true}]

But why and how do I fix this?

Craig W.
  • 17,838
  • 6
  • 49
  • 82
user979331
  • 11,039
  • 73
  • 223
  • 418
  • I would advocate not building your JSON by hand, but serialising it from a C# object. – ADyson Aug 28 '18 at 15:49
  • Anyway, the possible causes of a 409 are mentioned here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409 and elsewhere on the web. Quite why your server returns that is not clear without seeing the server code, to be honest. Presumably it doesn't like something about your input. Is there any text returned with the status code which might help to explain it? – ADyson Aug 28 '18 at 15:51
  • BTW, you are not using cURL here, you're clearly using C# and HttpClient. cURL is an entirely separate piece of software. It can also be used to make HTTP requests, but you're not using it so I'm not sure why you tagged that or mentioned it at all. I have edited the question to correct your terminology. – ADyson Aug 28 '18 at 15:52
  • 1
    Does the API you are calling provide documentation? They should document the possible return statuses and reasons for them. – Craig W. Aug 28 '18 at 17:50

1 Answers1

1

Rather than trying to manually build the JSON string, consider an approach like this.

string firstname = "";
string lastName = "";
string phoneNumber = "";
string primary = "";
string phoneNumber2 = "";

var registrant = new
{
    firstName = firstname,
    lastName = lastName,
    phones = new[]
    {
        new { phone = phoneNumber, type = "Home", primary = true },
        new { phone = phoneNumber2, type = "Work", primary = false }
    }
};

JavaScriptSerializer js = new JavaScriptSerializer();
string jsonData = js.Serialize(registrant);

Structuring your request in a way that you can more easily self-troubleshoot, may help you answer your own question and find out specifically which part of the data is causing the error. It will also help you avoid any basic typos when you're building the JSON.

The 409 could be anything. Check the response object for a human readable error message that might contain more information. In general, it means that your updated data conflicts with something. The phones, the addresses, etc. Start with a known working request and add elements one at a time.

If you can narrow down specifically which data is causing the server to return 409, then go back and look more carefully at their API documentation. You're on the right track.

Matt
  • 436
  • 3
  • 6