Android Question parameters - Content-Type "json" to "x-www-form-urlencoded"

fasilosman

Active Member
Licensed User
Longtime User
HI,

I am using PostSting for POST data using curl link. the server does not support "Contant-type : application/json". Therefore I use 'Content-type': 'application/x-www-form-urlencoded'

In PostString method I pass parameter for json type =
[{"email":"[email protected]","username":"ooaa112"}]

same above in urlencoded content-type :
"email=[email protected]&username=ooaa112"

The above working fine. But I don't know how to user the following json parameter in urlencorded type

[{"email":"[email protected]","username":"ooaa112","billing": {"address_1": "969 Market","address_2": "", "city": "San Francisco"}}]

I can make up-to this further I don't know how to continue
"email=[email protected]&username=ooaa112"&"billing=???"
 

OliverA

Expert
Licensed User
Longtime User
billing={"address_1": "969 Market","address_2": "", "city": "San Francisco"}

I have no clue what needs to be escaped. If you stick with Multipart posting, it would take care of it for you.

B4X:
Dim D As Map
D.Initialize
D.Put("email","[email protected]")
D.Put("username","aqqa112")
D.Put("password","aqqf")
D.Put("billing",$"{"address_1": "969 Market","address_2": "", "city": "San Francisco"}"$)

Dim Job As HttpJob
Job.Initialize("Register",Me)
Job.Username = "your_client_key"
Job.Password = "your_client_secret"
Job.PostMultipart("https://bcmcampus.000webhostapp.com/wp-json/wc/v2/customers" , D,Null)

This may work, you'll just have to try.
 
Upvote 0

fasilosman

Active Member
Licensed User
Longtime User
billing={"address_1": "969 Market","address_2": "", "city": "San Francisco"}

I have no clue what needs to be escaped. If you stick with Multipart posting, it would take care of it for you.

B4X:
Dim D As Map
D.Initialize
D.Put("email","[email protected]")
D.Put("username","aqqa112")
D.Put("password","aqqf")
D.Put("billing",$"{"address_1": "969 Market","address_2": "", "city": "San Francisco"}"$)

Dim Job As HttpJob
Job.Initialize("Register",Me)
Job.Username = "your_client_key"
Job.Password = "your_client_secret"
Job.PostMultipart("https://bcmcampus.000webhostapp.com/wp-json/wc/v2/customers" , D,Null)

This may work, you'll just have to try.


Thanks for the support. I will try.

I found some php code(below) for this but I have very poor knowledge in php, so I don't understand.
PHP:
/* the following serialize and param methods are a version of zepto's
* https://github.com/madrobby/zepto/blob/601372ac4e3f98d502c707bf841589fbc48a3a7d/src/ajax.js#L344-L370
* modified to use _ (tested on [email protected], should also work in underscore)
* to use with github's fetch:
     fetch(urrl, {
     method: 'post',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    body: params({key1: "value1", key2: "value2"})
     })
*/
  
  function serialize(params, obj, traditional, scope){
    var type, array = _.isArray(obj), hash = _.isObject(obj)
    _.each(obj, function(value, key) {
      type = typeof value
      if (scope) key = traditional ? scope :
        scope + '[' + (hash || type == 'object' || type == 'array' ? key : '') + ']'
      // handle data in serializeArray() format
      if (!scope && array) params.add(value.name, value.value)
      // recurse into nested objects
      else if (type == "array" || (!traditional && type == "object"))
        serialize(params, value, traditional, key)
      else params.add(key, value)
    })
  }

  function param(obj, traditional){
    var params = []
    params.add = function(key, value) {
      if (_.isFunction(value)) value = value()
      if (value == null) value = ""
      this.push(encodeURIComponent(key) + '=' + encodeURIComponent(value))
    }
    serialize(params, obj, traditional)
    return params.join('&').replace(/%20/g, '+')
  }


But finally I mange to go with PostString with ContantType "application/joson".
An hour age I found what was the issue. And I was preparing to post a reply. But you reply before me. Thanks

ISSUE -

when I was trying to generate the Map data (array data) as json string using JSONGenerator, it gives the string staring and ending character with "[" & "]".
The server I tried is not accepting this as a json string. The server expects the json string should begin and end with "{" & "}". So I removed the invalid character and send the PostString with json data. It worked. :).

I'm not sure that, it is a problem with the server I use, or it is a common issue. May be some one has experience with this or expert or Erel can explain this.

Any way I will try your code and let you know whether it is working or not.

Thank for helping me with this issue.
 
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
The square brackets around an item indicate that it's an array, so what you're sending is an array containing a single object. The server is expecting a single object, which is denoted by the "{" and "}"

In an ideal world, the server would be more accepting, and check to see if it was passed an array with a single object, or just a single object. Either carry on doing what you're doing, or make sure you get a single object to turn into JSON, rather than an array with one object in it.
 
Upvote 0
Top