Results 1 to 5 of 5

Thread: HTTP POST utf8

  1. #1

    HTTP POST utf8

    Beste,

    Ik stuur naar de API van Mailchimp een JSON bestand. Dit gaat in principe goed, krijg een keurige 200 code en alle gegevens worden verwerkt.
    Echter, het gaat fout bij mensen met een speciale achternaam, met een "é" erin bijvoorbeeld. Dat staat er in Mailchimp een ? bij de é.

    Een deel van het antwoord van Mailchimp:
    MailChimp uses 'UTF-8 (Unicode)' character encoding -- this means the likeliest cause of this problem is that the module's API call is encoded with non-UTF-8 formatting, which is preventing special characters like "é" from displaying correctly in your list. (Encoding is usually set as a 'content-type' or 'charset' value in the Headers of your API call, rather than the Request Body.)
    In Indy heb ik op ene paar plekken utf-8 ingevuld maar weet eigenlijk niet precies waar?

    Hebben jullie een idee? Waar vind ik de header van een idhttp.post ?

    Dank!

    Rogier

  2. #2
    Een aanvulling vanuit mailchimp:

    While I'm not personally familiar with Indy and Delphi, I do want to help as much as I possibly can. I did some further research on this, and it looks like UTF-8 can indeed be set in the header of your JSON. In the header, you should see a "content-type" declaraction -- this needs to have the "charset=utf-8" value added after application/json, so it would look like:

    "Content-type: application/json; charset=utf-8"

    Once this proper content-type is in the header of your JSON / API calls, you should see special characters like é passed correctly into MailChimp.

  3. #3
    Fornicatorus Formicidae VideoRipper's Avatar
    Join Date
    Mar 2005
    Location
    Vicus Saltus Orientalem
    Posts
    5,708
    In principe *zou* het voldoende moeten zijn als je zegt:
    Delphi Code:
    1. [...]
    2.   IdHTTP1.Request.CharSet := 'utf-8';
    3.   IdHTTP1.Request.ContentType := 'application/json';
    4. [...]

    ...als ik me niet vergis.
    TMemoryLeak.Create(Nil);

  4. #4
    Dank!

    Ik heb nu het volgende gedaan:

    Procedure Tformmaintestbed.btnJSONSendClick(Sender: TObject);
    var
    Json: string;
    sResponse: string;
    JsonToSend: TStringStream;
    begin
    Json := '{"auth": {"applicationId": "' + edApplication.text +
    '","applicationPassword": "' + edpassword.text +
    '","accountId": "' + edaccount.text +
    '","userId": "' + edUser.text +
    '"}}';

    memoRequest.Text := Json;

    JsonToSend := TStringStream.Create(Utf8Encode(Json)); // D2007 and earlier only
    //in D2009 and later, use this instead:
    //JsonToSend := TStringStream.Create(Json, TEncoding.UTF8);
    try
    HTTP1.Request.ContentType := 'application/json';
    HTTP1.Request.CharSet := 'utf-8';

    try
    sResponse := HTTP1.Post(cbAddress.Text, JsonToSend);
    except
    on E: Exception do
    ShowMessage('Error on request: '#13#10 + e.Message);
    end;
    finally
    JsonToSend.Free;
    end;

    memoResponse.Text := sResponse;
    end;
    Bron: http://stackoverflow.com/questions/2...http-post-json

  5. #5
    Stijn Sanders develyoy's Avatar
    Join Date
    Jun 2008
    Location
    GentBrugge, Belgi?½
    Posts
    1,046
    Hmm, het is niet genoeg met gewoon te zeggen dat het UTF8 zou zijn. Het moet ook echt UTF8 zijn! Ik raad ten zeerste aan intern WideString te gebruiken waar mogelijk, en naar idHTTP zelf eerst UTF8Encode te gebruiken!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •