Einzelnen Beitrag anzeigen

mikel.pahl

Registriert seit: 21. Sep 2015
Ort: Karlsruhe Neureut
5 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#14

AW: post Json mit REST

  Alt 5. Nov 2018, 13:09
Der Server ist nicht zickig, sondern erwartet ein application/json.
Du schickst ihm aber ein application/x-www-form-urlencoded.
Hallo Schokohase und BBommel,

es ist wohl leider so, dass der Server als ContentType nur nix oder application/x-www-form-urlencoded akzeptiert. Im Demoprogramm von denen war es auch mit "application/x-www-form-urlencoded".
Wenn Wireshark das Jsonobject erkennt gehts beim Server nicht. Das ist widersinnig aber ich kann es nicht ändern. Von der Theorie her habt ihr Recht aber der Server hält sich nicht dran.

Wenn ich das mit der RESTKomponten - wie gerade eben von Schokohase beschrieben - mache
- mit ctnone kommt 'Invalid JSON primitive:

- mit ctAPPLICATION_JSON sieht in Wireshark so aus:
Code:
Frame 8: 416 bytes on wire (3328 bits), 416 bytes captured (3328 bits) on interface 0
Ethernet II, Src: WistronI_bc:19:16 (3c:97:0e:bc:19:16), Dst: Cisco_27:f3:80 (00:26:0a:27:f3:80)
Internet Protocol Version 4, Src: 10.203.40.2, Dst: 148.253.138.2
Transmission Control Protocol, Src Port: 56837, Dst Port: 80, Seq: 294, Ack: 1, Len: 362
[2 Reassembled TCP Segments (655 bytes): #7(293), #8(362)]
Hypertext Transfer Protocol
    POST /ApiXRest/RestAPI/AccountPurses HTTP/1.1\r\n
    Connection: Keep-Alive\r\n
    Content-Type: application/json\r\n
    Accept: application/json, text/plain; q=0.9, text/html;q=0.8,\r\n
    Accept-Charset: UTF-8, *;q=0.8\r\n
    User-Agent: Embarcadero RESTClient/1.0\r\n
    Content-Length: 362\r\n
    Host: api.countersolutions.com\r\n
    \r\n
    [Full request URI: http://api.countersolutions.com/ApiXRest/RestAPI/xxxxx]
    [HTTP request 1/1]
    [Response in frame: 15]
    File Data: 362 bytes
JavaScript Object Notation: application/json
und als Fehlermeldung kommt in einem HTML-Body zurück:
The exception message is 'Incoming message for operation 'UpdateAccountPurse' (contract 'IRestAPI' with namespace 'http://tempuri.org/') contains an unrecognized http body format value 'Json'. The expected body format value is 'Raw'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.'


Mit
sResponse:=NetHTTPRequest1.Post(BaseURL+'XXXURL',jsonToSend); und die Proterty auf "application/x-www-form-urlencoded" dann ist der Server zufrieden.
In Wireshark sieht man, dass alle Header bis auf Content-Type fehlen:
Code:
Frame 5: 416 bytes on wire (3328 bits), 416 bytes captured (3328 bits) on interface 0
Ethernet II, Src: WistronI_bc:19:16 (3c:97:0e:bc:19:16), Dst: Cisco_27:f3:80 (00:26:0a:27:f3:80)
Internet Protocol Version 4, Src: 10.203.40.2, Dst: 148.253.138.2
Transmission Control Protocol, Src Port: 55896, Dst Port: 80, Seq: 216, Ack: 1, Len: 362
[2 Reassembled TCP Segments (577 bytes): #4(215), #5(362)]
Hypertext Transfer Protocol
    POST /ApiXRest/RestAPI/AccountPurses HTTP/1.1\r\n
    Connection: Keep-Alive\r\n
    Content-Type: application/x-www-form-urlencoded\r\n
    User-Agent: Embarcadero URI Client/1.0\r\n
    Content-Length: 362\r\n
    Host: api.countersolutions.com\r\n
    \r\n
    [Full request URI: http://api.countersolutions.com/ApiXRest/RestAPI/AccountPurses]
    [HTTP request 1/2]
    [Response in frame: 8]
    [Next request in frame: 11]
    File Data: 362 bytes
HTML Form URL Encoded: application/x-www-form-urlencoded
    Form item: "{"ApiXRequest":{"Hash":"067697798a41bfa6ff2d60f75ca2ca9d987e87010228b9640f288b59795fb3c6","BusinessID":"14","CustomerID":"129","SiteID":"1","LocationID":"40","DeviceID":"206","MessageNumber":"1050","UTC":"2018-11-02T12:04:10Z",
        Key [truncated]: {"ApiXRequest":{"Hash":"067697798a41bfa6ff2d60f75ca2ca9d987e87010228b9640f288b59795fb3c6","BusinessID":"14","CustomerID":"129","SiteID":"1","LocationID":"40","DeviceID":"206","MessageNumber":"1050","UTC":"2018-11-02T12:04
        Value:
So ein Request ohne Header bekomme ich mit der Restkomponten von Delphi nicht hin. Muss auch nicht. Ich bin mit der Lösung so glücklich.

Vielen Dank für den Input von euch.

Michael
Michael Pahl
  Mit Zitat antworten Zitat