Status 400: Bad request when sending special symbols

Comments

5 comments

  • Avatar
    Dmitry Dyudeev

    Hello,

    Could you please send us the whole createMessage request that fails to work? Special symbols shouldn't cause any issues with creating a notification.

    Aside from that, please note that our API supports non-ASCII symbols only in the "\uXXXX" format, so we generally recommend to use the following code:

    var dataString = JSON.stringify(data).replace(/[\u0080-\uFFFF]/g, function(m) { return "\u" + ("0000" + m.charCodeAt(0).toString(16)).slice(-4); });

  • Avatar
    ikrumov

    Here is the request:

     {
          "request": {
            "application": "XXXXX-XXXXX",
            "notifications": [
              {
                "apns_trim_content": 1,
                "conditions": [
                  [
                    "accountId",
                    "EQ",
                    4
                  ],
                  [
                    "userId",
                    "EQ",
                    4
                  ]
                ],
                "content": "tester@gmail.com : special characters: \u00f6\u00f6\u00fc\u00fc\u00f3\u00f3\r\n",
                "ios_badges": 1,
                "send_date": "now",
                "data": {
                  "caseId": 706,
                  "caseIdStr": "CVJ-39730"
                },
                "platforms": [
                  1,
                  3
                ]
              },
              {
                "apns_trim_content": 1,
                "conditions": [
                  [
                    "accountId",
                    "EQ",
                    4
                  ],
                  [
                    "userId",
                    "EQ",
                    118
                  ]
                ],
                "content": "tester@gmail.com : special characters: \u00f6\u00f6\u00fc\u00fc\u00f3\u00f3\r\n",
                "ios_badges": 1,
                "send_date": "now",
                "data": {
                  "caseId": 706,
                  "caseIdStr": "CVJ-39730"
                },
                "platforms": [
                  1,
                  3
                ]
              },
              {
                "apns_trim_content": 1,
                "conditions": [
                  [
                    "accountId",
                    "EQ",
                    4
                  ],
                  [
                    "userId",
                    "EQ",
                    123
                  ]
                ],
                "content": "tester@gmail.com : special characters: \u00f6\u00f6\u00fc\u00fc\u00f3\u00f3\r\n",
                "ios_badges": 1,
                "send_date": "now",
                "data": {
                  "caseId": 706,
                  "caseIdStr": "CVJ-39730"
                },
                "platforms": [
                  1,
                  3
                ]
              },
              {
                "apns_trim_content": 1,
                "conditions": [
                  [
                    "accountId",
                    "EQ",
                    4
                  ],
                  [
                    "userId",
                    "EQ",
                    132
                  ]
                ],
                "content": "tester@gmail.com : special characters: \u00f6\u00f6\u00fc\u00fc\u00f3\u00f3\r\n",
                "ios_badges": 1,
                "send_date": "now",
                "data": {
                  "caseId": 706,
                  "caseIdStr": "CVJ-39730"
                },
                "platforms": [
                  1,
                  3
                ]
              }
            ],
            "auth": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
          }
        }
    
  • Avatar
    Dmitry Dyudeev

    The strange thing is that we have just tested the request you provided, and it worked absolutely fine. You can see latest pushes on our demo account (username: demo, password: pushwoosh). Could you please double check whether this is the request that fails to work?

  • Avatar
    ikrumov

    It is. I only changed "auth", application code and the content of the message to include "tester@gmail.com" instead of a real address. I verified and the email change is not the problem. The request is sent to "https://cp.pushwoosh.com/json/1.3/createMessage"

    Other requests that do not include special characters succeed.

  • Avatar
    chrisn

    Hi, I'm ikrumov's college and we've solved the problem.

    We copied your sample code here (Java)

    http://www.pushwoosh.com/programming-push-notification/pushwoosh-push-notification-remote-api/#PushserviceAPI-JavaSample

    and this approach caused our Linux server not to encode the request properly. We solved the problem by using HttpClient. This is how we finally coded the request

    Client client = Client.create();
            client.setConnectTimeout(30 * 1000);
            client.setReadTimeout(50 * 1000);
            WebResource webResource = client.resource(requestUrl);
    
            ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class, requestData.toString());
    
            InputStream in = response.getEntityInputStream();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
    
            String jsonSrc = null;
            try {
                if (in.available() > 0) {
                    ReaderWriter.writeTo(in, out);
    
                    byte[] requestEntity = out.toByteArray();
                    jsonSrc = new String(requestEntity, Charset.forName("UTF-8"));
                }
            } catch (IOException ex) {
                throw new ContainerException(ex);
            }
    
            if(jsonSrc == null || jsonSrc.isEmpty()) {
                return null;
            }
            return new JSONObject(jsonSrc);
    

    I hope this helps other people who has similar problem

Please sign in to leave a comment.