2

I am trying to set up if-match header as following and making use of HttpClient available in System.Net.Http:

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var adobeRequest = new HttpRequestMessage(HttpMethod.Put, new Uri(url))
{
    Content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json")
};

if (!string.IsNullOrEmpty(Etag))
    adobeRequest.Headers.Add("If-Match", Etag);

var response = client.SendAsync(adobeRequest);

Etag I received from adobe in previous(Get) call is :

64E8BBA87ACFD0C2C84AF6E1193A3761.5334C3A18AB5A054FF3DBC33AFBDF6C

So when I try to add the same for Put request, it gives me following error:

The format of value '64E8BBA87ACFD0C2C84AF6E1193A3761.5334C3A18AB5A054FF3DBC33AFBDF6C' is invalid.

How to resolve this issue? It clearly says format is not valid, however I believe Adobe's api is being used by million others. So somehow Its something from my end.

Link for Adobe api

Screenshot while debugging

Gautam
  • 363
  • 1
  • 4
  • 15
  • Could someone please let me know why this question was down voted? – Gautam Mar 14 '19 at 18:55
  • The Adobe API looks like it takes the e-tag as a parameter named `If-Match` instead of a header, at least in `put /agreements/{agreementId}/state`. Have you tried adding the value as a parameter named `If-Match` instead of a header? – Jonathon Chase Mar 14 '19 at 18:57
  • What is the actual value of `Etag`? Totally guessing, but it's normal for the value itself to be quoted, which would then cause problems if you passed that as is into `Add` as it would attempt to add its own quotes around it, i.e. if the value is something like `"64E...F6C"`, then the header would end up being `""64E...FC6""`, which would be invalid. – Chris Pratt Mar 14 '19 at 18:59
  • @JonathonChase: Nope I did not try however on its description they have said to specify as Header. Let me try adding it as parameter.Thanks Jonathon. – Gautam Mar 14 '19 at 18:59
  • Alternatively (still guessing), maybe `Add` isn't adding quotes so the header ends up as `If-Match: 64E...F6C`, which would also be invalid. In that case, you would need to quote it yourself: `adobeRequest.Headers.Add("If-Match", $"\"{Etag}\"");` – Chris Pratt Mar 14 '19 at 19:02
  • @Chris Pratt: I have added the screenshot while debugging to show the value. – Gautam Mar 14 '19 at 19:05
  • Saw that. Thanks. Unfortunately it's a bit more difficult to tell exactly what's being sent out the wire, unless you've got Fiddler running or something. You might want to try adding the quotes manually as I suggested. May not work, but you might get lucky. – Chris Pratt Mar 14 '19 at 19:07

1 Answers1

2

Use adobeRequest.Headers.TryAddWithoutValidation instead.

Xiaoy312
  • 14,292
  • 1
  • 32
  • 44