PartnerCenterSamples / Commerce-API-Java

Java sample code for calling Cloud Solution Provider CREST APIs
2 stars 10 forks source link

Issue with special characters in address fields or customer fields in request json #1

Open bp89 opened 8 years ago

bp89 commented 8 years ago

Below is sample of failing request body:

{"status":"ACTIVE","name":"banana11","role":"RETAILCUSTOMER","language":"ENGLISH","activationDate":"2016-05-11T19:59:29+0530","groupAddress":{"phoneNumber":"53453545","zipCode":"13010111","state":"fsdfsdf","addressLine1":"fdsfsd","country":"BR","city":"SÃO PAULO"},"parent":{"id":6325},"assignedRepId":214,"customPropertiesSets":[{"specification":{"id":4},"configuration":{"microsoft_info":{"company":{"region":"","zip":"","purchasesContact":{"phone":"","email":"","name":""},"nfeContact":{"phone":"","email":"","name":""},"subject":{"westconAccountManager":"","taxID":"","tradeName":"","companyType":""},"organisation":{"sector":"","buildingName":"","website":"","segment":"","municipalTaxCode":0,"stateCode":false,"icmsContributor":false,"streetName":"","type":"Distribution","commisions":false,"simplesNacional":false,"stateTaxCode":"","district":""},"state":"","paymentsContact":{"phone":"","email":"","name":""},"address1":"","address2":"","companyName":"","city":""},"domain_prefix":"zasx","tid":" ","contact":{"first_name":"fdfdsfs","phone_number":"3455345543","email":"vinay.prajapati@tothenew.com","last_name":"fsdfds"}}}}]}

Everything works well till we don't have characters from different languages. But when we have those it fails. See city in request body above.

bp89 commented 8 years ago

Well the issue is in the the https://github.com/PartnerCenterSamples/Commerce-API-Java/blob/master/src/microsoft/partner/csp/api/v1/samples/Customer.java file at 267.

StringEntity class by default encodes to ISO charset and not to UTF and hence that was causing following error:

"error_code":"MissingContent","message":"The request was expected to be accompanied by data but no content was provided.","object_type":”Error

as data never reached to crest api due to error or something else.

To fix this rather than using

StringEntity se = new StringEntity(customerId.toString()); use

StringEntity se = new StringEntity(customerId.toString(),null,StandardCharsets.UTF_8.toString()); It will only work with Java7 as StandardCharsets are supported since java 7.

In case you are using older version, kindly use HTTP.UTF_8 (deprecated as of now).