Skip to content

Commit ecf038d

Browse files
authored
Merge pull request #348 from eyupfidan/master
Fetch
2 parents 124f041 + df0d19a commit ecf038d

File tree

1 file changed

+94
-93
lines changed

1 file changed

+94
-93
lines changed

5-network/01-fetch-basics/article.md

Lines changed: 94 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11

2-
# Fetch: Basics
2+
# Fetch: Temel Bilgiler
33

4-
Method `fetch()` is the modern way of sending requests over HTTP.
4+
`fetch()` yöntemi, HTTP üzerinden istek göndermenin modern bir yoludur.
55

6-
It evolved for several years and continues to improve, right now the support is pretty solid among browsers.
6+
Birkaç yılda geliştirildi ve geliştirilmeye devam ediliyor, şu anda tarayıcılar arasında desteği oldukça sağlam.
77

8-
The basic syntax is:
8+
Temel sözdizimi şöyledir:
99

1010
```js
1111
let promise = fetch(url, [options])
1212
```
1313

14-
- **`url`** -- the URL to access.
15-
- **`options`** -- optional parameters: method, headers etc.
14+
- **`url`** -- erişilecek URL.
15+
- **`options`** -- isteğe bağlı parametreler: method, headers vb.
1616

17-
The browser starts the request right away and returns a `promise`.
17+
Tarayıcı isteği başlatır ve bir `promise` döndürür.
1818

19-
Getting a response is usually a two-stage process.
19+
Yanıt alma işlemi genellikle iki aşamalıdır.
2020

21-
**The `promise` resolves with an object of the built-in [Response](https://fetch.spec.whatwg.org/#response-class) class as soon as the server responds with headers.**
21+
**Sunucu başlıklarını yanıtladığında, `promise` otomatik olarak oluşturulmuş [Response](https://fetch.spec.whatwg.org/#response-class) sınıfının bir nesnesiyle çözümlenir.**
2222

23+
Bu şekilde HTTP durumunu kontrol edebiliriz (status), başlıkları kontrol edebiliriz (head), ancak henüz gövdeyi (body) alamayız.
2324

24-
So we can check HTTP status, to see whether it is successful or not, check headers, but don't have the body yet.
25+
`fetch`, HTTP isteği yapamadığında (örneğin, ağ sorunları varsa veya böyle bir site yoksa)
26+
`promise `, reddeder. 404 veya 500 gibi HTTP hataları bile normal akış olarak kabul edilir.
2527

26-
The promise rejects if the `fetch` was unable to make HTTP-request, e.g. network problems, or there's no such site. HTTP-errors, even such as 404 or 500, are considered a normal flow.
28+
Bunları response özelliklerinde görebiliriz:
2729

28-
We can see them in response properties:
30+
- **`ok`** -- boolean, HTTP durum kodu 200-299 ise true döndürür.
31+
- **`status`** -- HTTP durum kodu.
2932

30-
- **`ok`** -- boolean, `true` if the HTTP status code is 200-299.
31-
- **`status`** -- HTTP status code.
32-
33-
For example:
33+
Örneğin:
3434

3535
```js
3636
let response = await fetch(url);
@@ -43,94 +43,95 @@ if (response.ok) { // if HTTP-status is 200-299
4343
}
4444
```
4545

46-
To get the response body, we need to use an additional method call.
46+
Response gövdesini (body) almak için, ek bir yöntem çağrısı yapmamız gerekiyor.
47+
48+
`Response` gövdesine erişmek için birden fazla format ve özellik vardır:
4749

48-
`Response` provides multiple promise-based methods to access the body in various formats:
50+
- **`response.json()`** -- yanıtı JSON nesnesi olarak ayrıştırır,
51+
- **`response.text()`** -- yanıtı metin (text) olarak döndürür,
52+
- **`response.formData()`** -- yanıtı FormData nesnesi olarak döndürür (form/çok parçalı kodlama),
53+
- **`response.blob()`** -- yanıtı Blob türünde döndürür [Blob](info:blob) (binary data tipi (ikili)),
54+
- **`response.arrayBuffer()`** -- yanıtı [ArrayBuffer](info:arraybuffer-binary-arrays) türünde döndürür (saf ikili veri),
55+
- ek olarak, `response.body` bir, [ReadableStream](https://streams.spec.whatwg.org/#rs-class) nesnesidir, gövdeyi parça parça okumaya izin verir, daha sonra bir örnek göreceğiz.
4956

50-
- **`response.json()`** -- parse the response as JSON object,
51-
- **`response.text()`** -- return the response as text,
52-
- **`response.formData()`** -- return the response as FormData object (form/multipart encoding),
53-
- **`response.blob()`** -- return the response as [Blob](info:blob) (binary data with type),
54-
- **`response.arrayBuffer()`** -- return the response as [ArrayBuffer](info:arraybuffer-binary-arrays) (pure binary data),
55-
- additionally, `response.body` is a [ReadableStream](https://streams.spec.whatwg.org/#rs-class) object, it allows to read the body chunk-by-chunk, we'll see an example later.
5657

57-
For instance, here we get a JSON-object with latest commits from GitHub:
58+
Örneğin, burada GitHub'dan en son commitleri içeren bir JSON nesnesi alıyoruz:
5859

5960
```js run async
6061
let response = await fetch('https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits');
6162

6263
*!*
63-
let commits = await response.json(); // read response body and parse as JSON
64+
let commits = await response.json(); // yanıt gövdesini okuyun ve JSON olarak ayrıştırın
6465
*/!*
6566

6667
alert(commits[0].author.login);
6768
```
6869

69-
Or, the same using pure promises syntax:
70+
Ya da promises sözdizimi kullanarak aynısını yapabilirsiniz:
7071

7172
```js run
7273
fetch('https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits')
7374
.then(response => response.json())
7475
.then(commits => alert(commits[0].author.login));
7576
```
7677

77-
To get the text:
78+
Metni almak için:
7879
```js
7980
let text = await response.text();
8081
```
8182

82-
And for the binary example, let's fetch and show an image (see chapter [Blob](info:blob) for details about operations on blobs):
83+
Ve binary örneği için, bir görsel getirelim ve gösterelim (bloblar üzerindeki işlemler hakkında ayrıntılar için [Blob](info:blob) bölümüne bakın):
8384

8485
```js async run
8586
let response = await fetch('/article/fetch/logo-fetch.svg');
8687

8788
*!*
88-
let blob = await response.blob(); // download as Blob object
89+
let blob = await response.blob(); // Blob nesnesi olarak indirme
8990
*/!*
9091

9192
// create <img> for it
9293
let img = document.createElement('img');
9394
img.style = 'position:fixed;top:10px;left:10px;width:100px';
9495
document.body.append(img);
9596

96-
// show it
97+
// Gösterme
9798
img.src = URL.createObjectURL(blob);
9899

99-
setTimeout(() => { // hide after two seconds
100+
setTimeout(() => { // 2 saniye sonra gizle
100101
img.remove();
101102
URL.revokeObjectURL(img.src);
102103
}, 2000);
103104
```
104105

105-
````warn
106-
We can choose only one body-parsing method.
107-
108-
If we got the response with `response.text()`, then `response.json()` won't work, as the body content has already been processed.
106+
```warn
107+
Yalnızca bir gövde ayrıştırma yöntemi seçebiliriz.
108+
```
109+
Yanıtı `response.text()` ile aldıysak, gövde içeriği zaten işlenmiş olduğundan `response.json()` çalışmayacaktır.
109110

110-
```js
111-
let text = await response.text(); // response body consumed
112-
let parsed = await response.json(); // fails (already consumed)
111+
````js
112+
let text = await response.text(); // yanıt gövdesi çevirildi
113+
let parsed = await response.json(); // hata (yukarıda zaten çevirilmişti)
113114
````
114115

115-
## Headers
116+
## Başlıklar (Headers)
116117

117-
There's a Map-like headers object in `response.headers`.
118+
`response.headers` içinde Map benzeri bir headers nesnesi vardır.
118119

119-
We can get individual headers or iterate over them:
120+
Başlıkları tek tek alabilir veya üzerinde yineleme yapabiliriz:
120121

121122
```js run async
122123
let response = await fetch('https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits');
123124

124-
// get one header
125+
// sadece bir başlık alma
125126
alert(response.headers.get('Content-Type')); // application/json; charset=utf-8
126127

127-
// iterate over all headers
128+
// tüm başlıkları çekme
128129
for (let [key, value] of response.headers) {
129130
alert(`${key} = ${value}`);
130131
}
131132
```
132133

133-
To set a header, we can use the `headers` option, like this:
134+
Bir başlık ayarlamak için aşağıdaki `headers` seçeneğini deneyebilirsiniz:
134135

135136
```js
136137
let response = fetch(protectedUrl, {
@@ -139,8 +140,7 @@ let response = fetch(protectedUrl, {
139140
}
140141
});
141142
```
142-
143-
...But there's a list of [forbidden HTTP headers](https://fetch.spec.whatwg.org/#forbidden-header-name) that we can't set:
143+
...Ancak ayarlayamayacağımız yasaklı [HTTP başlıklarının](https://fetch.spec.whatwg.org/#forbidden-header-name) bir listesi var:
144144

145145
- `Accept-Charset`, `Accept-Encoding`
146146
- `Access-Control-Request-Headers`
@@ -163,24 +163,24 @@ let response = fetch(protectedUrl, {
163163
- `Proxy-*`
164164
- `Sec-*`
165165

166-
These headers ensure proper and safe HTTP, so they are controlled exclusively by the browser.
166+
Bu başlıklar düzgün ve güvenli HTTP isteği sağlar, bu nedenle yalnızca tarayıcı tarafından kontrol edilirler.
167167

168-
## POST requests
168+
## POST istekleri
169169

170-
To make a `POST` request, or a request with another method, we need to use `fetch` options:
170+
`POST` isteği veya başka bir yöntemle istek yapmak için `fetch` seçeneklerini kullanmamız gerekir:
171171

172-
- **`method`** -- HTTP-method, e.g. `POST`,
173-
- **`body`** -- one of:
174-
- a string (e.g. JSON),
175-
- `FormData` object, to submit the data as `form/multipart`,
176-
- `Blob`/`BufferSource` to send binary data,
177-
- [URLSearchParams](info:url), to submit the data as `x-www-form-urlencoded`, rarely used.
172+
- **`method`** -- HTTP-methodu, örn. `POST`,
173+
- **`body`** -- örnekler:
174+
- string değer (örn. JSON),
175+
- `FormData` nesnesi olarak göndermek için `form/multipart`,
176+
- `Blob`/`BufferSource` ikili (binary) veri göndermek için,
177+
- [URLSearchParams](info:url), verileri `x-www-form-urlencoded`, göndermek için, nadiren kullanılır.
178178

179-
Let's see examples.
179+
Hadi örneklere bakalım.
180180

181-
## Submit JSON
181+
## Json Veri Göndermek
182182

183-
This code submits a `user` object as JSON:
183+
Bu kod bir `user` objesini JSON olarak gönderir:
184184

185185
```js run async
186186
let user = {
@@ -202,11 +202,11 @@ let result = await response.json();
202202
alert(result.message);
203203
```
204204

205-
Please note, if the body is a string, then `Content-Type` is set to `text/plain;charset=UTF-8` by default. So we use `headers` option to send `application/json` instead.
205+
Kısa bir not, göndereceğim değer string ise, `Content-Type` değerini `text/plain;charset=UTF-8` olarak belirlememiz gerekiyor. Json veri göndereceğimiz için `headers` objesine `application/json` özelliğini ekliyoruz.
206206

207-
## Submit a form
207+
## Form verisini göndermek
208208

209-
Let's do the same with an HTML `<form>`.
209+
Aynı şeyi bir HTML `<form>`'u ile yapalım.
210210

211211

212212
```html run
@@ -231,13 +231,15 @@ Let's do the same with an HTML `<form>`.
231231
</script>
232232
```
233233

234-
Here [FormData](https://xhr.spec.whatwg.org/#formdata) automatically encodes the form, `<input type="file">` fields are handled also, and sends it using `Content-Type: form/multipart`.
234+
Burada [FormData](https://xhr.spec.whatwg.org/#formdata) formu otomatik olarak encode eder, `<input type="file">` alanları işlenir ve, `Content-Type: form/multipart` olarak gönderir.
235235

236-
## Submit an image
236+
## Görselleri göndermek
237237

238-
We can also submit binary data directly using `Blob` or `BufferSource`.
238+
Görsel verisini gönderirken ikili (binary) veri olarak göndermemiz gerekir.
239239

240-
For example, here's a `<canvas>` where we can draw by moving a mouse. A click on the "submit" button sends the image to server:
240+
Direkt olarak `Blob` veya `BufferSource` kullanarak gönderebiliriz.
241+
242+
Örneğin, burada fareyi hareket ettirerek çizim yapabileceğimiz bir `<canvas>` var. "Gönder" butonuna tıklandığında görsel sunucuya gönderilir:
241243

242244
```html run autorun height="90"
243245
<body style="margin:0">
@@ -266,9 +268,9 @@ For example, here's a `<canvas>` where we can draw by moving a mouse. A click on
266268
</body>
267269
```
268270

269-
Here we also didn't need to set `Content-Type` manually, because a `Blob` object has a built-in type (here `image/png`, as generated by `toBlob`).
271+
Burada ayrıca `Content-Type` manuel olarak ayarlamamız gerekmedi, çünkü `Blob` nesnesinin yerleşik bir türü vardır (burada `toBlob` tarafından oluşturulan `image/png`).
270272

271-
The `submit()` function can be rewritten without `async/await` like this:
273+
Ayrıca `submit()` fonksiyonu `async/await` olmadan şu şekilde yazabiliriz:
272274

273275
```js
274276
function submit() {
@@ -283,11 +285,11 @@ function submit() {
283285
}
284286
```
285287

286-
## Custom FormData with image
288+
## Görsel içeren Formdata'yı göndermek
287289

288-
In practice though, it's often more convenient to send an image as a part of the form, with additional fields, such as "name" and other metadata.
290+
Pratikte bir görseli "name" ve diğer meta veriler gibi ek alanlarla birlikte formun bir parçası olarak göndermek genellikle daha uygundur.
289291

290-
Also, servers are usually more suited to accept multipart-encoded forms, rather than raw binary data.
292+
Ayrıca, sunucular genellikle ham ikili veriler yerine çok parçalı kodlanmış formları(multipart-encoded forms) kabul etmeye daha uygundur.
291293

292294
```html run autorun height="90"
293295
<body style="margin:0">
@@ -323,39 +325,38 @@ Also, servers are usually more suited to accept multipart-encoded forms, rather
323325
</body>
324326
```
325327

326-
Now, from the server standpoint, the image is a "file" in the form.
328+
Şimdi, sunucu açısından bakıldığında, görsel formdaki bir "dosya "dır.
327329

328-
## Summary
330+
## Özet
329331

330-
A typical fetch request consists of two `awaits`:
332+
Tipik bir fetch isteği iki bölümden oluşur `awaits`:
331333

332334
```js
333-
let response = await fetch(url, options); // resolves with response headers
334-
let result = await response.json(); // read body as json
335-
```
335+
let response = await fetch(url, options); // başlık kurallara göre okunur
336+
let result = await response.json(); // gövdeyi json olarak geri döndürü
336337

337-
Or, promise-style:
338+
Veya, promise stilinde:
338339
```js
339340
fetch(url, options)
340341
.then(response => response.json())
341342
.then(result => /* process result */)
342343
```
343344

344-
Response properties:
345-
- `response.status` -- HTTP code of the response,
346-
- `response.ok` -- `true` is the status is 200-299.
347-
- `response.headers` -- Map-like object with HTTP headers.
345+
Yanıt(Response) Özellikleri
346+
- `response.status` -- HTTP durum kodunu içerir,
347+
- `response.ok` -- `true` ise değer 200-299 arası olmalıdır
348+
- `response.headers` -- Başlıklarını içeren bir map benzeri nesne döndürür
348349

349-
Methods to get response body:
350-
- **`response.json()`** -- parse the response as JSON object,
351-
- **`response.text()`** -- return the response as text,
352-
- **`response.formData()`** -- return the response as FormData object (form/multipart encoding),
353-
- **`response.blob()`** -- return the response as [Blob](info:blob) (binary data with type),
354-
- **`response.arrayBuffer()`** -- return the response as [ArrayBuffer](info:arraybuffer-binary-arrays) (pure binary data),
350+
Yanıt Gövdesini (Response Body) alma yöntemleri:
351+
- **`response.json()`** -- yanıtı bir JSON objesine çevirir,
352+
- **`response.text()`** -- yanıtı bir text olarak döndürür,
353+
- **`response.formData()`** -- yanıtı bir FormData objesi olarak döndürür (form/multipart encoding),
354+
- **`response.blob()`** -- yanıtı [Blob](info:blob) (binary data tipi) olarak döndürür,
355+
- **`response.arrayBuffer()`** -- yanıtı [ArrayBuffer](info:arraybuffer-binary-arrays) (saf ikili veri (binary)) olarak döndürür,
355356

356-
Fetch options so far:
357-
- `method` -- HTTP-method,
358-
- `headers` -- an object with request headers (not any header is allowed),
359-
- `body` -- string/FormData/BufferSource/Blob/UrlSearchParams data to submit.
357+
Şimdiye kadarki "Fetch" seçenekleri:
358+
- `method` -- HTTP methodları (POST,GET vs,),
359+
- `headers` -- istek başlıklarını içeren bir nesne (herhangi bir başlığa izin verilmez),
360+
- `body` -- string/FormData/BufferSource/Blob/UrlSearchParams türünde gönderilecek veriler
360361

361-
In the next chapters we'll see more options and use cases.
362+
Sonraki bölümlerde daha fazla seçenek ve kullanım durumu göreceğiz.

0 commit comments

Comments
 (0)