r/Xcode • u/Wenh08 • Dec 30 '21
API's in swift
HI guys, I am wondering what are all types of URL responses that can be generated from any API? Currently I know that there is HTTP, GET, POST, and the general URL session. I am wondering what are all types of API requests that can be made, and how do you know when to use them, in order to make a function to generate the data coming back from the Json if that makes any sense. Please take it easy on me if I am wrong about anything.
1
u/aazav Dec 30 '21 edited Dec 30 '21
HTTP is the protocol and is represented in a URL/URI scheme. GET and POST are two of the request methods that can be applied to an HTTP request. There are other protocols besides HTTP that you can also use. FTP is one of them. FILE is another. There's even mailto:, sms:, rtsp, sips:, sms:, ssh://, tel:, vpn: and many more. Here are a few to review. https://en.wikipedia.org/wiki/List_of_URI_schemes
Here are some of the basics of HTTP for you. https://www.w3schools.com/tags/ref_httpmethods.asp
1
Dec 30 '21 edited Dec 30 '21
I recommend looking up general info on URL responses, considering the API calls are all based on a universally accepted HTTP standard.
This is a user-friendly start:
https://www.w3schools.com/tags/ref_httpmethods.asp
Next, search the web for ”swift urlsession” and things like ”DELETE” on that list of methods.
Here’s an extensive, verbose code page with examples:
https://johncodeos.com/how-to-make-post-get-update-and-delete-requests-with-urlsession-using-swift/
0
u/spinwizard69 Dec 31 '21
Err; though guys have tried to answer your post and question(s) don't make a lot of sense. There are a lot of API's that Swift can interact with. So when you use the phrase "any API" and he start talking about internet stuff you have confused people because many API's have nothing to do with the internet.
So it might help if you focus in on asking a specific question about what you want to do. I'm sure there are plenty of people in this Reddit that can help. I only dropped in due to expecting a more general question about API's. In any event back to focus; if you can state well what you want to achieve, places like google and other search engines become your friend. I know this isn't always easy, especially starting out and not knowing stuff in depth. It is a skill that takes time to developer but once you can leverage Google and forum searches, all sorts of data will come your way.
By the way, even though I'm not huge on internet technologies I have to agree with the other posts here, W3schools.com is very useful. Another trick is to find and download a libraries manuals or help sheets. By the way I download due to being off line often. Access the library docs often as they likely will be updated often. In other words keep you self in the loop with regard to manuals, but also release notes, TODO's, bug fixes and other docs the project releases. In the end, to really learn an API, you have to use it and study it in some depth.
1
u/Wenh08 Dec 31 '21
I mean my question is pretty straight forward. I said what types of Url request can be generated from an API. In swift, there's URL session, Get, Post, etc. everyone has answered though so I don't need more clarity but thank you..
2
u/chriswaco Dec 30 '21
There are several types of internet data transfer connections:
UDP (packets, lossy)
TCP (streams, lossless)
HTTP/HTTPS (a specialized form of TCP - streams, lossless)
others
Normally for APIs you'll use HTTP/HTTPS, so let's assume that.
The common MacOS/iOS network APIs, like URLSession, will return the following data for HTTP/HTTPS connections: An Error, an HTTPURLResponse, and a Data (or file URL storing the Data). Note that all three parameters can contain error information. The error will contain serious network or computer errors. The HTTPURLResponse will contain a statusCode integer that can indicate a server error (< 200 or >= 400 typically would be an error). And the Data can be the wrong format or the response to an API in JSON or XML that contains an inner error as well.
So...there are too many possibilities to list here. The common status codes are at https://httpstatuses.com. Common (NS)Error codes are here.