Skip to content

Commit 45fe5df

Browse files
committed
Add JSON as default Accept
1 parent 2dff417 commit 45fe5df

4 files changed

Lines changed: 31 additions & 9 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The API supports DDI-specific response formats only. Use the `Accept` header to
3232

3333
**DDI JSON Format (default):**
3434
```bash
35-
# DDI JSON format (default if no Accept header)
35+
# DDI JSON format (default - no Accept header needed)
3636
curl https://api.example.com/ddi/v1/variables
3737

3838
# Explicit DDI JSON request
@@ -41,12 +41,12 @@ curl -H "Accept: application/vnd.ddi.structure+json;version=3.3" https://api.exa
4141

4242
**DDI XML Format:**
4343
```bash
44-
# DDI XML format
44+
# DDI XML format (requires Accept header)
4545
curl -H "Accept: application/vnd.ddi.structure+xml;version=3.3" https://api.example.com/ddi/v1/variables
4646
```
4747

4848
**Supported Content Types:**
49-
- `application/vnd.ddi.structure+json;version=3.3` - DDI JSON format (default)
49+
- `application/vnd.ddi.structure+json;version=3.3` - DDI JSON format (default if no Accept header)
5050
- `application/vnd.ddi.structure+xml;version=3.3` - DDI XML format
5151

5252
**Note:**

ddi-rest.yaml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ info:
99
1010
DDI is an international standard for describing statistical and social science data.
1111
12+
## Response Formats
13+
14+
The API supports DDI-specific response formats:
15+
- `application/vnd.ddi.structure+json;version=3.3` - DDI JSON format (default if no Accept header is provided)
16+
- `application/vnd.ddi.structure+xml;version=3.3` - DDI XML format
17+
18+
If no `Accept` header is provided, the API returns DDI JSON format by default.
19+
1220
servers:
1321
- description: Mock API Server
1422
url: http://ddi-api.making-sense.info/ddi/v1
@@ -54,6 +62,8 @@ paths:
5462
Variables describe the structure of data elements within a dataset. They include information such as name, label, description, and references to associated concepts.
5563
5664
Variables can be filtered by various criteria including study, dataset, or concept references.
65+
66+
**Response Format**: If no `Accept` header is provided, returns DDI JSON format (`application/vnd.ddi.structure+json;version=3.3`) by default.
5767
parameters:
5868
- $ref: "#/components/parameters/urn"
5969
- $ref: "#/components/parameters/agencyID"
@@ -84,6 +94,8 @@ paths:
8494
- An ID (if agencyID and version are provided as query parameters)
8595
8696
Returns detailed information about the variable including its structure, concepts, and related metadata.
97+
98+
**Response Format**: If no `Accept` header is provided, returns DDI JSON format (`application/vnd.ddi.structure+json;version=3.3`) by default.
8799
parameters:
88100
- $ref: "#/components/parameters/variableID-path"
89101
- $ref: "#/components/parameters/agencyID"
@@ -885,7 +897,10 @@ components:
885897

886898
responses:
887899
"200-variables":
888-
description: OK - List of variables
900+
description: |
901+
OK - List of variables.
902+
903+
**Default format**: If no `Accept` header is provided, returns DDI JSON format (`application/vnd.ddi.structure+json;version=3.3`).
889904
content:
890905
application/vnd.ddi.structure+xml;version=3.3:
891906
description: |
@@ -933,6 +948,7 @@ components:
933948
</l:Variable>
934949
</g:ResourcePackage>
935950
application/vnd.ddi.structure+json;version=3.3:
951+
description: DDI JSON format (default if no Accept header is provided)
936952
schema:
937953
type: array
938954
items:
@@ -986,6 +1002,7 @@ components:
9861002
</l:Variable>
9871003
</g:ResourcePackage>
9881004
application/vnd.ddi.structure+json;version=3.3:
1005+
description: DDI JSON format (default if no Accept header is provided)
9891006
schema:
9901007
$ref: "#/components/schemas/Variable"
9911008
"200-concepts":

docs/MOCK_API_ENDPOINTS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ The mock API supports DDI-specific formats only:
2121
- **DDI JSON** (default): `application/vnd.ddi.structure+json;version=3.3`
2222
- **DDI XML**: `application/vnd.ddi.structure+xml;version=3.3`
2323

24-
The format is determined by the `Accept` header in your request. If no `Accept` header is provided, DDI JSON format is returned by default.
24+
The format is determined by the `Accept` header in your request.
25+
26+
**Default Behavior**: If no `Accept` header is provided (or if `Accept: */*` is sent), the API returns DDI JSON format by default. You don't need to specify the `Accept` header to get JSON responses.
2527

2628
**Note:** Generic formats (`application/json`, `application/xml`, `text/xml`) are not supported and will return a `406 Not Acceptable` error.
2729

mocks/server.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,17 +366,20 @@ function resolveReferences(obj, level, startDepth = 0) {
366366
// Helper to determine response format based on Accept header
367367
function getResponseFormat(req) {
368368
const accept = req.headers.accept || '';
369+
370+
// If no Accept header or Accept is */*, default to DDI JSON
371+
if (!accept || accept === '*/*' || accept.trim() === '') {
372+
return 'json';
373+
}
374+
369375
// Only accept DDI-specific formats
370376
if (accept.includes('application/vnd.ddi.structure+xml;version=3.3')) {
371377
return 'xml';
372378
}
373379
if (accept.includes('application/vnd.ddi.structure+json;version=3.3')) {
374380
return 'json';
375381
}
376-
// Default to JSON DDI format if no Accept header
377-
if (!accept || accept === '*/*') {
378-
return 'json';
379-
}
382+
380383
// Unsupported format
381384
return null;
382385
}

0 commit comments

Comments
 (0)