Networking with URLSession - Part 2: HTTP 101 | Ray Wenderlich

Learn some HTTP basics to make the most of URLSession, and discover the easy way to create URL objects from Strings.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4321-networking-with-urlsession/lessons/2

Hi Audrey,
I’m relatively new to Swift and have just started the URLSession, but have already hit a bump.
Approximately at 6.26 of the video where you created a db.json file and pasted the example code, when I do the same, Terminal doesn’t allow me to return or do any other functions after. I can’t find any options to make a plain text version anywhere too.
I’ve tried restarting, deleting the file and repeating your instructions with no success. Any help would be appreciated! Thank you!

hi xiatran!

I use vi, but you can open TextEdit, the editor that comes with macOS.

If a new file has a toolbar that lets you set font, font size etc, use the menu to switch it to plain text:

TextEdit>Format>Make Plain Text

I also prefer to make it wrap to window:

TextEdit>Format>Wrap to Window

@xiatran Thanks very much for your question!

A nice text editor that is also free and open source is Atom.

I hope that helps!

All the best!

1 Like

Thank you @Audrey and @syedfa for both your advice!
I managed to make the .json file using TextEdit and it’s running now cheesy grin.

onwards and upwards!

1 Like

Just a small suggestion. Using the parameter “posts” is a little confusing with the HTTP action “POST” esp. for someone looking at the Web Servers for the first time. Just may be changing it to something else might make the concept more clearer and easier to grasp.

Also if you want to use vi or vim (great tools to have in the arsenal as a programmer if you wanna tinker in terminal and not worry about touching that mouse/trackpad :slight_smile: ),
Once you paste or type what you want in the file, you can hit Esc Key followed by : key to enter the command mode from text editing mode. The screen on bottom left will let you type commands. Some of the exit commands are as below.
:w (Saves changes made so far. Similar to File -> Save)
:q (Quit - Note - Any changes made after last w will not be saved)
:wq (Save and Quit)

thanks for these suggestions, Dev! the “posts” thing kind of niggled at me, too. And I normally use vi (learned from Bill Joy’s original tutorial sheets!) but I know most people prefer less cryptic editors.

1 Like

Yeah … vi has the old school charm … people who got started on it love it … “new age” developers seems to hate it … just my observation :smiley:

@dev,
it is a form over function and ease of use. The older paradigm was text based, where as the newer paradigm is UI based, so what used to be possible with keystrokes is now possible with UI and menu. If anyone remembers how the precursors to Excel used a / to call up functions. Now everything is simply Visual.

However having said that, there are a lot of young guns that love some of the older keyboard utilities, I think it is more about a mindset than age.

cheers,

Jayant

In the challenge i don’t understand why you entered http://localhosts/posts/2 – why 2 ?
I attempted to put in the parameters before PUTting.
id: 2
author: joe blogg
title: blah blah

This is clearly wrong bc it failed but why …/posts/2 ?

PUT replaces the data at a URL with what you put in the body. The id 2 is part of the URL, specifying which posts resource you want to update. If you don’t include 2 in the URL, the host doesn’t know which resource you want to update.

Also, the host assigns the id when you POST, and this value should not be changed via a PUT request, which is what could happen if you could include id as part of the data in the body.

Hope this helps.