Web Connection
Adding a charset declaration to an HTTP post request
Gravatar is a globally recognized avatar based on your email address. Adding a charset declaration to an HTTP post request
  Richard Kaye
  All
  Mar 31, 2020 @ 09:25am

I have a request from the devs I'm working with on this endpoint of theirs. They have asked me if I can add a charset to the application/json header. As best I can tell, there doesn't seem to be a way to do that? Basically I hit this code in wwhttp.httpgetex:

IF llHasPostData
   DO CASE
      *** Explicit content type overrides manual content type
      CASE !EMPTY(this.cContentType)
 	tcHeaders = "Content-Type: " + this.cContentType + CRLF + tcHeaders      

this.cContentType is "application/json". How do I get "; charset=UTF-8" to be appended to that? I tried setting lDecodeUTF8 to .t. in my http object but the class seems to create its own local instance and I'm not sure that gets passed through or if it's even relevant?

Gravatar is a globally recognized avatar based on your email address. re: Adding a charset declaration to an HTTP post request
  Richard Kaye
  Richard Kaye
  Mar 31, 2020 @ 12:19pm

So I read the fine documentation and it implies that I could explicitly set cContenType after instantiating my http object. However, HttpGetEx still seems to declare its own local object and my explicit setting of that property did not seem to carry thorugh.

Gravatar is a globally recognized avatar based on your email address. re: Adding a charset declaration to an HTTP post request
  Rick Strahl
  Richard Kaye
  Mar 31, 2020 @ 01:20pm

This should do it:

loHttp.AppendHeader("Content-Type","application/json; charset=utf-8")

Or simply:

loHttp.cContentType = "application/json; charset=utf-8"

cContentType should alwyas be applied as long as there is POST data to send and it will override the nHttpPostMode property if that's set.

++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Adding a charset declaration to an HTTP post request
  Richard Kaye
  Rick Strahl
  Mar 31, 2020 @ 03:39pm

I'll try expclictly adding the header and let you know.

Thanks.

Gravatar is a globally recognized avatar based on your email address. re: Adding a charset declaration to an HTTP post request
  Richard Kaye
  Rick Strahl
  Mar 31, 2020 @ 04:09pm

When I append the header, as opposed to setting cContentType, the final post object ends up with 2 ContentType headers. Is that valid?

I'm happy to record another debugging session if that's useful.

Gravatar is a globally recognized avatar based on your email address. re: Adding a charset declaration to an HTTP post request
  Rick Strahl
  Richard Kaye
  Mar 31, 2020 @ 04:22pm

No but it's also not something that you would normally do. Do one or the other but not both.

Setting cContentType is the right way to to do this.

+++ Rick ---

© 1996-2024