jeudi 30 juin 2016

How do I read JSON on my Golang server that I've posted from iOS using NSData?


I'm trying to validate a receipt by sending it to my custom server from iOS.

I have my NSMutableURLRequest and set it up as so:

    let body: [String: AnyObject] = ["receipt": receipt, "prod_id": productID]

    let optionalJson: NSData?

    do {
        optionalJson = try NSJSONSerialization.dataWithJSONObject(body, options: [])
    } catch _ {
        optionalJson = nil
    }

    guard let json = optionalJson else { return }

    request.HTTPBody = json

Then I send it off to my server which is written in Go, but I don't know how to then read the data.

Previously I sent only the data (in raw form, not a JSON structure), and then turned into a base 64 encoded string as so before shipping it off:

data, _ := ioutil.ReadAll(request.Body)
encodedString := base64.StdEncoding.EncodeToString(data)

But now I the JSON structure that links a prod_id with is a string, as well as the receipt data, which I assume is bytes. How do I extract that into readable JSON then turn it into a base 64 encoded string as above?

I guess I'm not sure what structure the JSON would take.


Aucun commentaire:

Enregistrer un commentaire