WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#39755 closed defect (bug) (worksforme)

Rest API json string corruption?

Reported by: sgehrman Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7.2
Component: REST API Keywords:
Focuses: Cc:
PR Number:

Description

I set up a new install of WP. Register a rest route. It returns json with string with spaces in it.

I expect to get back: {"wtf": 'w t f'}
But I get {"wtf" => 'w t f'}

I call $.post() from my client.

Why are the multiple spaces in my string converted to one space? Data corruption!

I spent the day googling this, and didn't find anyone who could explain why this is happening. Is there some config I don't know about that needs to be setup to not corrupt the data?

<?php
add_action( 'rest_api_init',   'setupRESTApi' );

function setupRESTApi() {
        register_rest_route( 'api', '/rest', array(
                'methods'  => WP_REST_Server::CREATABLE,
                'callback' => 'endpoint_rest'
        ) );
}

// WP_REST_Request
function endpoint_rest( $request ) {
         $result = ["wtf" => 'w                      t                          f'];

        return rest_ensure_response( $result );
}

Change History (9)

#1 @rmccue
3 years ago

Hi @sgehrman and welcome to Trac :)

I cannot replicate this locally:

$ curl -X POST vagrant.local/wp-json/api/rest
{"wtf": "w                      t                          f"}

It's possible that the way you're viewing the data is displaying this with spaces compressed, or something in $.post() is altering this. If you're outputting this data into HTML, keep in mind that HTML compacts multiple spaces into a single one.

#2 follow-up: @rmccue
3 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

#3 in reply to: ↑ 2 @sgehrman
3 years ago

Replying to rmccue:

curl works on my computer also. Thanks!

But for some reason, I get back collapsed spaces in the result of $.post(). I'm console logging it. Would that collapse the spaces? I also do compares in javascript and it fails unless I get rid of the spaces, so I'm not sure when the data is getting changed.

Version 0, edited 3 years ago by sgehrman (next)

#4 follow-up: @rmccue
3 years ago

I'm not certain on how jQuery.post() works, you'd have to check their documentation. In any case, as this isn't an issue with the REST API, but rather with using the data from it, your question is better asked on the support forums. :)

#5 in reply to: ↑ 4 ; follow-up: @sgehrman
3 years ago

Replying to rmccue:

I'm not certain on how jQuery.post() works, you'd have to check their documentation. In any case, as this isn't an issue with the REST API, but rather with using the data from it, your question is better asked on the support forums. :)

I couldn't figure out the forums. That page doesn't have a forum and I've been seaching for it and failed, so I did a bug report.

This ticket was mentioned in Slack in #meta by rmccue. View the logs.


3 years ago

#7 follow-up: @rmccue
3 years ago

That is the forums. The links on that page are to the various categories of the forums, try the Developing with WordPress forum.

#8 in reply to: ↑ 5 @Otto42
3 years ago

Replying to sgehrman:

I couldn't figure out the forums. That page doesn't have a forum and I've been seaching for it and failed, so I did a bug report.

There are 9 separate links on that page which all say "View Forum". Each of them leads to a different forum, each of which has a different main topic.

Installing WordPress
Fixing WordPress
Developing with WordPress
Networking WordPress
Accessibility
Localhost Installs
Everything else WordPress
Requests and Feedback
Alpha/Beta/RC

In addition to that, every plugin and theme has its own "forum" under its individual "Support" tab. You can post about plugins or themes there.

#9 in reply to: ↑ 7 @sgehrman
3 years ago

I figured it out!!

I had installed some stupid html minifier plugin and it was minifying my REST responses. Stupid. I knew it had to be some crazy bug or plugin or something. There's no way this isn't going to work rock solid.

Thanks!

Note: See TracTickets for help on using tickets.