diepm / vim-rest-console

A REST console for Vim.
658 stars 54 forks source link

Automatic format response does not work for ContentType with charset #82

Open xvitcoder opened 5 years ago

xvitcoder commented 5 years ago

In case the ContentType: application/json;charset=UTF-8, i.e. it contains the charset, the automatic format of response does not work.

I think the proble is in the regex from rest.vim:668 does not substitute the charset

let fileType = substitute(contentType, '\v^.*/(.*\+)?(.*)$', '\2', 'g')

calling

substitute('application/json;charset=UTF-8', '\v^.*/(.*\+)?(.*)$', '\2', 'g')

returns json;charset=UTF-8 and not json which is the key in s:vrc_auto_format_response_patterns map.

xvitcoder commented 5 years ago

After more analysis of the code, it seems that the content type is found earlier in the code, but the output is still not formatted.

Here is my rest response

HTTP/1.1 100 

HTTP/1.1 100 

HTTP/1.1 200 
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, PUT, PATCH, OPTIONS, DELETE
Access-Control-Allow-Headers: X-Requested-With, Origin, Accept, Content-Type, Authorization
Access-Control-Max-Age: 1728000
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
vary: accept-encoding
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 16 Oct 2019 08:33:41 GMT

{"productList":[{"id":301265,"descShort":"Reeh Cuvée Sweet Reeh 2015","descLong":"Reeh Cuvée Sweet Reeh 2015, Einheit: 750 ML","ean":"9120048143192","deliveryTime":null,"keywords":null,"supplier":{"id":1,"name":"First Supplier","partNum":"935499"},"manufacturer":{"partNum":null,"name":""},"catalog":{"id":14,"versionId":11,"name":"AGM"},"order":{"orderUnit":"BO","contentUnit":"FL","packingQuantity":1.0,"priceQuantity":0.0,"quantityMin":1.0,"quantityInterval":null},"customerPartNumbers":null,"documents":null,"images":[{"type":"image/jpeg","path":"/media/1/14/11/935499.jpg","description":"Produktbild","purpose":null,"order":1}],"prices":[{"type":"net_list","units":1,"amount":6.7,"currency":"EUR","tax":20.0,"priceFactor":1.0,"isDailyChangeable":false,"validFromQuantity":null,"validFromDate":"2019-09-18T21:00:00.000+0000","validToDate":null,"territories":null}],"fees":null,"attributes":[{"name":"Rechtlich vorgeschriebene Produktbezeichnung","value":"SWEET REEH (ZW/ME) 15 REEH"},{"name":"Füllgewicht","value":"750 Milliliter"},{"name":"Ursprungsland","value":"Österreich"},{"name":"verpflichtende Kennzeichnung an der Ware","value":"Österreichischer Qualitätswein"},{"name":"Inhaltsstoffe","value":"Enthält Sulfite"},{"name":"Allergene","value":"O"}],"classifications":[{"type":"ECLASS","version":"9.1","code":"16120290"}],"externalSource":null,"externalData":{}},{"id":301274,"descShort":"Reeh Cuvée Sweet Reeh 2015","descLong":"Reeh Cuvée Sweet Reeh 2015, Einheit: 750 ML","ean":"9120048143208","deliveryTime":null,"keywords":null,"supplier":{"id":1,"name":"First Supplier","partNum":"935499"},"manufacturer":{"partNum":null,"name":""},"catalog":{"id":14,"versionId":11,"name":"AGM"},"order":{"orderUnit":"C62","contentUnit":"FL","packingQuantity":6.0,"priceQuantity":0.0,"quantityMin":1.0,"quantityInterval":null},"customerPartNumbers":null,"documents":null,"images":[{"type":"image/jpeg","path":"/media/1/14/11/935499.jpg","description":"Produktbild","purpose":null,"order":1}],"prices":[{"type":"net_list","units":1,"amount":37.2,"currency":"EUR","tax":20.0,"priceFactor":1.0,"isDailyChangeable":false,"validFromQuantity":null,"validFromDate":"2019-09-18T21:00:00.000+0000","validToDate":null,"territories":null}],"fees":null,"attributes":[{"name":"Rechtlich vorgeschriebene Produktbezeichnung","value":"SWEET REEH (ZW/ME) 15 REEH"},{"name":"Füllgewicht","value":"750 Milliliter"},{"name":"Ursprungsland","value":"Österreich"},{"name":"verpflichtende Kennzeichnung an der Ware","value":"Österreichischer Qualitätswein"},{"name":"Inhaltsstoffe","value":"Enthält Sulfite"},{"name":"Allergene","value":"O"}],"classifications":[{"type":"ECLASS","version":"9.1","code":"16120290"}],"externalSource":null,"externalData":{}}],"numFound":3,"numPages":2,"page":0,"pageSize":2,"took":66,"termsAggregations":{"supplierName":[{"value":3,"key":"First Supplier"}],"catalogName":[{"value":3,"key":"AGM"}],"manufacturerName":[{"value":3,"key":""}]},"statsAggregations":{"price":{"min":6.7,"avg":38.43333333333334,"max":71.4,"count":3,"sum":115.30000000000001}},"treeAggregations":{"attributes":[{"values":[{"count":3,"key":"O"}],"count":3,"key":"Allergene"},{"values":[{"count":3,"key":"750 Milliliter"}],"count":3,"key":"Füllgewicht"},{"values":[{"count":3,"key":"Enthält Sulfite"}],"count":3,"key":"Inhaltsstoffe"},{"values":[{"count":2,"key":"SWEET REEH (ZW/ME) 15 REEH"},{"count":1,"key":"ZWEIGELT UNPLUGGED 16 REEH"}],"count":3,"key":"Rechtlich vorgeschriebene Produktbezeichnung"},{"values":[{"count":3,"key":"Österreich"}],"count":3,"key":"Ursprungsland"}]}}

curl options

Accept: application/json
Content-Type: application/json
-i -s 
xvitcoder commented 5 years ago

I think the problem is in the multiple HTTP/1.1 100 which has empty lines in between. Tested with other responses which have just one HTTP/1.1 200 in response, the formatter works correctly.

Adding the header

Expect:

Will suppress the HTTP 100 repetition and the output will be formatted correctly. Still I think the code can be enhanced to ignore the HTTP lines

diepm commented 5 years ago

You're right that the extra HTTP/x with empty lines cause the issue. One way (while waiting for the enhancement) is to use vrc_header_content_type (buffer scope) if you already know the response content type.

xvitcoder commented 5 years ago

@diepm Thank you for fast response