0

I am working on a WordPress website (but I think that the problem is not strictly related to WordPress but to some string encoding problem).

Basically I have two post in a specific post type named "Notary districts". This post type have a custom field named wpcf-idnotary-district containing a string value representing the ID of an object.

Ok then I have two different POST having different format for the value of this wpcf-idnotary-district field.

I have the following situation:

POST 1: the value of wpcf-idnotary-district field is 123456 POST 2: the value of wpcf-idnotary-district field is CG7drXn9fvA%253D

Then I have an WordPress API retrieving post from this post type by this wpcf-idnotary-district ID value.

Getting the previous POST 1 from my post type using the first value it is not a problem, infact calling my API passing this ID paramether, something like this:

https://www.MYPORTAL.it/wp-json/filter/notary-district?wpcf-idnotary-district=123456

I am obtaining the expected response:

[
    {
        "post_type": "notary-district",
        "ID": 39244,
        "wpcf-idnotary-district": "123456",
        "post_title": "DISTRICT NAME TEST"
    }
]

the problem happen getting the previous POST 2 using this ID, infact calling:

https://www.MYPORTAL.it/wp-json/filter/notary-district?wpcf-idnotary-district=CG7drXn9fvA253D

in this case the JSON response is an empty array:

[]

But, as you can see here it exist a post with this wpcf-idnotary-district field having CG7drXn9fvA%253D as value:

enter image description here

Trying to get this post calling my API give me this issue (an empty list, it doesn't find the post) both calling my API via Postman and by a Java batch application that I am developing.

I suppose that the problem is the % character into the CG7drXn9fvA%253D value, infact if I try to replace this post ID with this CG7drXn9fvA253D value (the previous one without the % character), then trying to search to this new value it works fine. So the problem is this % character.

I suspect that it is converting it in some way. Maybe I can try to pass the converted value as my API parameter but I have no idea about what I can try.

Some idea? How can I try to solve this problem?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • Encode your value first, then send it. That's what browsers do! – Ruvee Sep 18 '21 at 11:13
  • @Ruvee how to encode? in what format? sending it encoded it should be accetpted by my called API? – AndreaNobili Sep 18 '21 at 15:08
  • You could encode it using [javascrip](https://stackoverflow.com/a/12042764/15040627) OR [php](https://stackoverflow.com/questions/4744888/how-to-properly-url-encode-a-string-in-php) OR any other language you're working with. And yes, wordpress will pick up on that, if not, you could decode it on the other side of your api (wordpress-side). – Ruvee Sep 18 '21 at 15:14
  • @Ruvee I can't modify the WordPress APIs because it was not developed by me (I can only call these APIs). Can you please show me what can I try to encode (API caller side)? what format? – AndreaNobili Sep 18 '21 at 15:22
  • 1
    You could use `javascript` like so: `let my_value ="CG7drXn9fvA%253D";encodeURI(my_value);`. Or you could use `php` like so: `$my_value ="CG7drXn9fvA%253D";urlencode($my_value);` – Ruvee Sep 18 '21 at 16:53

1 Answers1

1

CG7drXn9fvA%253D is the url encoded form of CG7drXn9fvA%3D,

% = %25

you can decode this in postman itself.

Just copy the text to any part like body , url etc select the text and right click and select decode.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
PDHide
  • 18,113
  • 2
  • 31
  • 46