Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP Request - GET/POST displayed in a view #37

Open
sohilpandya opened this issue Jun 2, 2017 · 5 comments
Open

HTTP Request - GET/POST displayed in a view #37

sohilpandya opened this issue Jun 2, 2017 · 5 comments
Assignees

Comments

@sohilpandya
Copy link
Member

Now that we have been able to make a get request on a playground #34, we will move on to make this information show up on a view alongside making it possible to make a post request.

We will be using httpbin for making the HTTP requests.

I'll write up a process as I go along creating this demo app.

@sohilpandya
Copy link
Member Author

sohilpandya commented Jun 4, 2017

I'm using https://github.com/typicode/json-server to create a local api which I can send get/post requests to.

I've set the db in a db.json file

{
  "ios" : {
      "project_name":"iOS",
      "total_time":0,
      "color":"blue"
  },
  "elm": {
      "project_name": "elm",
      "total_time":0,
      "color":"blue"
  }
}
  • GET this data show a part of this information on the screen.
  • POST data to this db.

@sohilpandya
Copy link
Member Author

I'm using json-server to create a simple db.json file that contains the following information:

{
  "store": [{
    "id": 0,
    "ios": {
      "project_name": "iOS",
      "total_time": 0,
      "color": "blue"
    }
  },{
    "id": 1,
    "elm": {
      "project_name": "elm",
      "total_time": 0,
      "color": "blue"
    }
  }]
}

Currently using localhost:3000 for getting and posting information

@sohilpandya
Copy link
Member Author

OK, so I've posted a question related to convering dictionaries to json here - https://stackoverflow.com/questions/44368070/dictionary-to-json-being-serialised-twice-in-swift-3

I can't seem to get it to post correctly, the format it appears in is incorrect as you can see in the code below

{
  "store": [
    {
      "id": 0,
      "ios": {
        "project_name": "iOS",
        "total_time": 0,
        "color": "blue"
      }
    },
    {
      "id": 1,
      "elm": {
        "project_name": "elm",
        "total_time": 0,
        "color": "blue"
      }
    },
    {
      "{\"newTask\":{\"project_name\":\"iOS\",\"total_time\":0,\"color\":\"blue\"}}": "",
      "id": 2
    }
  ]
}

@sohilpandya
Copy link
Member Author

sohilpandya commented Jun 5, 2017

@iteles could see that I was getting a bit deflated today because of this issue but someone has answered the question in Stack Overflow! 🎉 Can continue the work rather than putting it to the side!

@sohilpandya
Copy link
Member Author

We are now able make the POST request!

turns out we needed to specify that the context was in json using the following line:
request.setValue("application/json", forHTTPHeaderField: "Content-Type")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant